Escolar Documentos
Profissional Documentos
Cultura Documentos
OSI Layer
Application Presentation Session Transport Network Data Link Physical
Description
Application level protocol Encoding, Formatting Manages end-user comm. Transparent user data, reliability, manages packets Packets between source & destination, routing Data between nodes Bits on the medium (cable)
TCP/IP
HTTP, FTP
Comments
Calin Curescu
ISO (International Standards Organization) has created a layered model, called the OSI (Open Systems Interconnect) TCP/IP (Transmission Control Protocol / Internet Protocol) already developed
java.net package
Sockets One endpoint of a two-way communication link between two programs running on the network ServerSocket, Socket classes in java.net URL (Uniform Resource Locator) http://ida.liu.se:80/figure.jpg (protocol:resource) Connecting to resource Reading & writing (using input and output streams) Datagrams Needed for UDP communication TCP provides reliable flows, which are automatically transformed to InputStream and OutputStream
Server Example
import java.net.*; import java.io.*; public class EchoServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; //ports under 1024 are usually reserved by OS try { serverSocket = new ServerSocket(7); } catch (IOException e) { System.err.println("Could not listen on port: 7"); System.exit(1); } Socket clientSocket = null; //thread blocks until a client connects try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { out.println(inputLine); } out.close(); in.close(); clientSocket.close(); serverSocket.close(); } }
4. Client and server can communicate by using the InputStream and OutputStream provided by Socket Think about a pipe between server and client A pipe is uniquely identified by [server address, server port, client address, client port]
5 of 12 TDDC32 lecture 2, 2006
http://java.sun.com/docs/books/tutorial/networking/
Basic Networking in Java Calin Curescu 6 of 12 TDDC32 lecture 2, 2006
Client Example
import java.io.*; import java.net.*; public class EchoClient { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; //servers address and port //takes an available free local port BufferedReader in = null; try { echoSocket = new Socket("dalix.ida.liu.se", 7); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { //automatically tries to resolve System.err.println("Unknown host: dalix.ida.liu.se "); hostname to IP address (using a DNS) System.exit(1); } catch (IOException e) { System.err.println("IOException connecting to: dalix.ida.liu.se "); System.exit(1); //automatically tries connect to server } BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } out.close(); in.close(); stdIn.close(); echoSocket.close(); } }
Basic Networking in Java Calin Curescu 7 of 12 TDDC32 lecture 2, 2006
Multiple clients
On the server side:
while (true) { accept a connection ; //returning a new socket create a thread to deal with the client ; //i.e. that takes as an argument the new created socket }
Datagram Server
import java.io.*; import java.net.*; import java.util.*; public class EchoDatagramServer { public static void main(String[] args) throws IOException { protected DatagramSocket socket = new DatagramSocket(7777); while (true) { try { byte[] buf = new byte[256]; //receive request DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); //send the response to the client at "address" and "port" InetAddress address = packet.getAddress(); int port = packet.getPort(); packet = new DatagramPacket(buf, buf.length, address, port); socket.send(packet); } catch (IOException e) { e.printStackTrace(); } } socket.close(); } }
Datagram Client
import java.io.*; import java.net.*; public class EchoDatagramClient { public static void main(String[] args) throws IOException { if (args.length != 1) { System.out.println("Usage: java DatagramClient <hostname>"); System.exit(1); } //get a datagram socket DatagramSocket socket = new DatagramSocket(); String userInput; while ((userInput = stdIn.readLine()) != null) { //send request byte[] buf = userInput.getBytes(); InetAddress address = InetAddress.getByName(args[0]); DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 7777); socket.send(packet); //get response packet = new DatagramPacket(buf, buf.length); socket.receive(packet); //display response String received = new String(packet.getData()); System.out.println("echo: " + received); } socket.close(); } }
Pipes
Inter-thread communication Shared variables Method calls Shared files Pipe Streams
PipedWriter pipeOut = new PipedWriter(); PipedReader pipeIn = new PipedReader(pipeOut);
The End
RMI Remote method invocation Advanced networking technique Call methods on objects that are not on the same machine
Thread A
pipeOut
pipeIn
Thread B