Application Layer

2.7 Socket Programming with TCP

Home
Introduction
2.1 Principles of Network Applications
2.1.1 Network Application Architectures
2.1.2 Processes Communcating
2.1.3 Application-Layer Protocols
2.1.4 What Services Does an Application Need?
2.1.5 Services Provided by the Internet Transport Protocols
2.2 The Web and HTTP
2.2.1Overview of HTTP
2.2.2 Nonpersistent and Persistent Connections
2.2.3 HTTP Message Format
2.2.4 User-Server Ineraction: Cookies
2.2.5 HTTP Content
2.2.6 Web Caching
2.2.7 The Conditional GET
2.3 File Transfer: FTP
2.3.1 FTP Commands and Replies
2.4 Electronic Mail in the Internet
2.4.1 STMP
2.4.2 Comparison with HTTP
2.4.3 Mail Message Formats and MIME
2.4.4 Mail Access Protocols
2.5 DNS--The Internet's Directory Service
2.5.1 Services Provided by DNS
2.5.2 Overview of How DNS Works
2.5.3 DNS Records and Messages
2.6 P2P File Sharing
2.7 Socket Programming with TCP
2.7.1 Socket Programming with TCP
2.7.2 An Example Client/Server Application in Java
2.8 Socket Programming with UDP

2.7.2 An Example Client/Server Application in Java

The following simple client/server application to demonstrate socket programming for both TCP and UDP:
  1. A client reads a line from its standard input (keyboard) and sends the line out its socket to the server.
  2. The server reads a line from its connection socket.
  3. The server converts the line to uppercase.
  4. The server sends the modified line out its connection socket to the client.
  5. The client reads the modified line from its socket and prints the line on its standard output (monitor).
 
The client/server program pair for a TCP implementation of the application. Provide a detailed, line-by-line analysis after each program.  The client program is called TCPClient.java, and the server program is called TCPServer.java.  In order to emphasize the key issues provides code that is to the point but not bulletproof. "Good code" would certainly have a few more auxiliary lines.
 
Once the two programs are compiled on their respective hosts, the server program is first executed at the server host, which created a server porcess at the sercer host.  The server process waits to ve contacted by a client process.  When the client program is executed, a process is created at the client, and this process immediately contacts the server and establishes a TCP connection with it.  The user at the client then use the application to send a line and then receive a capitalized version of the line.
 
TCPClient.java
The TCPClient creates three streams and one socket.  The socket is called clientSocket.  The stream inFromUser is an input stream to the program; it is attached to the standard input.  When the user types characters on the keyboard, the characters flow into the stream inFromUser.  The stream inFromServer is another input stream to the program; it is attached to the socket.  Characters that arrive from the network flow into the stream inFromServer.  Finally, the stream outToServer is an output stream from the program; it is also attached to the socket.  Characters that the client sends to the network flow into the stream outToServer.

kurose_320719_c02f29.gif