2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens Calvin College
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(2/11) The TCP/IP Stack TCP/IP is a layered protocol family: Hardware Application layer protocols convert app. program data to/from messages FTPTELNETSMTP... messages Transport layer protocols convert messages to/from packets. TCPUDP packets The Internet layer protocol (IP) converts packets to/from IP datagrams IP datagrams The Network interface layer protocol (X.25) converts IP datagrams to/from network’s frames to send/receive... X.25 frames TCP/IP Applications data
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(3/11) host j host i TCP/IP app i TCP/IP app j Application Transport Internet Network Application Transport Internet Network network End-to-End Communication The stacks enable any two hosts to communicate in a point-to-point fashion. A TCP/IP network is a collection of communication links and hosts, with each host having a TCP/IP stack. Control info is also sent between specific layers... Each layer of the stack (at each end) performs its respective task. data messagepacketsdatagrams frames
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(4/11) host j host i network The Client-Server Model Most TCP/IP applications use the client-server model, splitting a network application into two parts: Servers for specific services use well-known ports: echo7 daytime13 quote17 ftp-data20 ftp-cntl21 telnet23 smtp25 dns53 finger79 A client that contacts a server to access the service it offers: client app An always-running server (or daemon) that provides a specific service: server app
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(5/11) Sockets To provide a service to clients, a server: creates a socket using a given protocol (TCP or UDP) and port waits for a client to send a request to the socket (blocking) receives a request via the socket, processes it sends any results back to the client via the socket To request service from a server, a client: creates a socket uses it to send a request for service to a server uses it to receive any results from the server Most TCP/IP applications communicate using an abstraction called a socket, that represents a communication endpoint. Sockets are the software infrastructure of TCP/IP networks.
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(6/11) Java Sockets ServerSocket is a class representing TCP server sockets, Socket is a class representing TCP client sockets, and DatagramSocket is a class representing UDP sockets, that provides send() and receive() methods by which clients and servers can send/receive DatagramPacket objects. Java provides separate classes to represent various kinds of sockets, including: Java also provides class InetAddress and other useful classes in its java.net package.
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(7/11) TCP Echo Client A client for the echo service is easy to write... import java.net.*; // Socket import java.io.*; // BufferedReader, PrintWriter,... class EchoClient { public static void main(String [] args) { //... Omitted: Code to input echo server’s name and port try { BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) ); PrintWriter screen = new PrintWriter( new OutputStreamWriter( System.out ), true ); // … continued on next slide …
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(8/11) TCP Echo Client (ii) //... continued from previous slide … Socket s = new Socket(InetAddress.getByName(remoteHost), remotePort); BufferedReader sin = new BufferedReader( new InputStreamReader( s.getInputStream() )); PrintWriter sout = new PrintWriter( new OutputStreamWriter( s.getOutputStream() ), true ); String lineFromUser = null, lineFromServer = null; for (;;) { lineFromUser = keyboard.readLine(); if ( lineFromUser.equals("quit") ) break; sout.println(lineFromUser); lineFromServer = sin.readLine(); screen.println(lineFromServer); } s.close(); } catch( Exception e ) { System.err.println(e); } } Note: This program does not follow the MVC pattern…
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(9/11) TCP Echo Client A client using the MVC pattern is also easy to write... import java.net.*; // Socket import java.io.*; // BufferedReader, PrintWriter,... class EchoClient2 { public static void main(String [] args) { //... Omitted: Code to input echo server’s name and port try { BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) ); PrintWriter screen = new PrintWriter( new OutputStreamWriter( System.out ), true ); // … continued on next slide … So far this is identical to the previous version…
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(10/11) TCP Echo Client (ii) //... continued from previous slide … EchoClient theClient = new EchoClient(remoteHost, remotePort); String lineFromUser = null, lineFromServer = null; for (;;) { lineFromUser = keyboard.readLine(); if ( lineFromUser.equals("quit") ) break; theClient.send(lineFromUser); lineFromServer = theClient.receive(); screen.println(lineFromServer); } However, This version isolates the core functionality of the client in a separate EchoClient class, and so the rest is much simpler than the previous version: The EchoClient class provides a constructor, plus simple send() and receive() methods…
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(11/11) Exercise: Part II Use the remaining time to work through part II of today’s exercise. We will be demo-ing your Part II solutions at the end of today’s session!