OSI and TCP/IP Layers Basic Networking in Java
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
TCP, UDP IP Network frames
Source and destination port numbers Source & destination IP numbers
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
Basic Networking in Java Calin Curescu
12 pages TDDC32 lecture 2, 2006
Basic Networking in Java Calin Curescu
2 of 12 TDDC32 lecture 2, 2006
TCP & UDP
TCP Connection based protocol Data flows i.e. IP Packets are ordered, FIFO Reliable IP Packets are retransmitted Congestion and flow control UDP Connectionless protocol Independent datagrams
Basically adding source and destination port to IP packet
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
May arrive in any order Unreliable Datagrams can be lost
Basic Networking in Java Calin Curescu 3 of 12 TDDC32 lecture 2, 2006
Basic Networking in Java Calin Curescu
4 of 12 TDDC32 lecture 2, 2006
Sockets for TCP
1. On server side, a socket (ServerSocket) is bound to a port and listens for incoming client connections 2. On client side, the server address and port is known; the client creates a new socket (Socket) and tries to connect to server 3. If everything ok On server side the server accepts the connection and creates a new socket (Socket) bound on a different port Server needs the initial socket to continue to listen On client side the connection attempt returns successfully
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
Examples from Java Tutorials:
http://java.sun.com/docs/books/tutorial/networking/
Basic Networking in Java Calin Curescu 6 of 12 TDDC32 lecture 2, 2006
Basic Networking in Java Calin Curescu
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 }
Basic Networking in Java Calin Curescu
8 of 12 TDDC32 lecture 2, 2006
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(); } }
Basic Networking in Java Calin Curescu
9 of 12 TDDC32 lecture 2, 2006
Basic Networking in Java Calin Curescu
10 of 12 TDDC32 lecture 2, 2006
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
Additional reading The Java Tutorial Trail: Custom Networking
http://java.sun.com/docs/books/tutorial/networking/index.html
or use PipedInputStream and PipedOutputStream
Basic Networking in Java Calin Curescu
11 of 12 TDDC32 lecture 2, 2006
Basic Networking in Java Calin Curescu
12 of 12 TDDC32 lecture 2, 2006