Question: Let's look at a simple example that illustrates how a program can establish a connection to a server program using the Socket class and then,
Let's look at a simple example that illustrates how a program can establish a connection to a server program using the Socket class and then, how the client can send data to and receive data from the server through the socket.
The example program implements a client, EchoClient, that connects to an echo server. The echo server receives data from its client and echoes it back. The example EchoServer implements an echo server. Alternatively the client can connect to any host that supports the Echo Protocol.
The EchoClient example creates a socket, thereby getting a connection to the echo server. It reads input from the user on the standard input stream, and then forwards that text to the echo server by writing the text to the socket. The server echoes the input back through the socket to the client. The client program reads and displays the data passed back to it from the server.
Note that the EchoClient example both writes to and reads from its socket, thereby sending data to and receiving data from the echo server.
Let's walk through the program and investigate the interesting parts. The following statements in the trywithresources statement in the EchoClient example are critical. These lines establish the socket connection between the client and the server and open a PrintWriter and a BufferedReader on the socket:
String hostName args;
int portNumber Integer.parseIntargs;
try
Socket echoSocket new SockethostName portNumber; st statement
PrintWriter out nd statement
new PrintWriterechoSocketgetOutputStream true;
BufferedReader in rd statement
new BufferedReader
new InputStreamReaderechoSocketgetInputStream;
BufferedReader stdIn th statement
new BufferedReader
new InputStreamReaderSystemin
The first statement in the trywith resources statement creates a new Socket object and names it echoSocket. The Socket constructor used here requires the name of the computer and the port number to which you want to connect. The example program uses the first commandline argument as the name of the computer the host name and the second command line argument as the port number. When you run this program on your computer, make sure that the host name you use is the fully qualified IP name of the computer to which you want to connect. For example, if your echo server is running on the computer echoserver.example.com and it is listening on port number first run the following command from the computer echoserver.example.com if you want to use the EchoServer example as your echo server:
java EchoServer
Afterward, run the EchoClient example with the following command:
java EchoClient echoserver.example.com
The second statement in the trywith resources statement gets the socket's output stream and opens a PrintWriter on it named out. Similarly, the third statement gets the socket's input stream and opens a BufferedReader on it named in The example uses readers and writers so that it can write Unicode characters over the socket. If you are not yet familiar with the Java platform's IO classes, you may wish to read Basic IO
The next interesting part of the program is the while loop. The loop reads a line at a time from the standard input stream with the BufferedReader object stdIn, which is created in the fourth statement in the trywith resources statement. The loop then immediately sends the line to the server by writing it to the PrintWriter connected to the socket:
String userInput;
while userInput stdIn.readLine null
out.printlnuserInput;
System.out.printlnecho: inreadLine;
The last statement in the while loop reads a line of information from the BufferedReader connected to the socket. The readLine method waits until the server echoes the information back to EchoClient. When readline returns, EchoClient prints the information to the standard output.
The while loop continues until the user types an endofinput character. That is the EchoClient example reads input from the user, sends it to the Echo server, gets a response from the server, and displays it until it reaches the endofinput. You can type an endofinput character by pressing CtrlC The while loop then terminates, and the Java runtime automatically closes the readers and writers connected to the socket and to the standard input stream, and it closes the socket connection to the server. The Java runtime closes these resources automatically because they were created in the trywithresources statement. The Java runtime closes these resources in reverse order that they were created. This is good because streams connected to a socket should be closed before the socket itself is closed.
This client progr
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
