The following simple client/server application to demonstrate socket programming for both TCP and UDP:
- A client reads a line from its standard input (keyboard) and sends the line out its socket to the server.
- The server reads a line from its connection socket.
- The server converts the line to uppercase.
- The server sends the modified line out its connection socket to the client.
- 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.