In this Tutorials section we cover and explain Reading Form writing into Socket in details for freshers and experienced
previous | Home | Next |
eading form and Writing into Socket
How a program can establish a connection to a server program using the Socket class and then, how can client and server communicate each other , and which type the client is send the request and server sends to response from client.
Through the example program implementing a client, EchoClient, that connects to the Echo server. Actually the Echo server simply receives data from its client and echoes it back. The Echo server is a well-known service that clients can connect with on port 7.
The EchoClient creates a socket there by it's getting a connection to the Echo server. and It reads input from the user on the standard input stream, and after that forwards that text to the Echo server by writing the text to the socket. The server is 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
Example
import java.io.*; import java.net.*; public class EchoClientSet { public static void main(String[] args) throws IOException { Socket echoSocketSet = null; PrintWriter outer = null; BufferedReader inner = null; try { echoSocketSet = new Socket("r4r", 7); outer = new PrintWriter(echoSocketSet.getOutputStream(), true); inner = new BufferedReader(new InputStreamReader( echoSocketSet.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: r4r."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: r4r."); System.exit(1); } BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } outer.close(); inner.close(); stdIn.close(); echoSocketSet.close(); } }
Note that EchoClient both writes to and reads from its socket, thereby sending data to and receiving data from the Echo server. The three statements in the try block of the main method are critical. These lines establish the socket connection between the client and the server and open a PrintWriter and a BufferedReader on the socket
echoSocketSet = new Socket("r4r", 7); outer = new PrintWriter(echoSocketSet.getOutputStream(), true); inner = new BufferedReader(new InputStreamReader( echoSocketSet.getInputStream()));
The first statement in this sequence creates a new Socket object and names it echoSocket. The Socket constructor used here requires the name of the machine and the port number to which you want to connect. The example program uses the host name r4r. This is the name of a hypothetical machine on our local network. When you type in and run this program on your machine, change the host name to the name of a machine on your network. Make sure that the name you use is the fully qualified IP name of the machine to which you want to connect. The second argument is the port number. Port number 7 is the port on which the Echo server listens.
The second statement gets the socket's output stream and opens a PrintWriter on it. Similarly, the third statement gets the socket's input stream and opens a BufferedReader on it. The example uses readers and writers so that it can write Unicode characters over the socket.
To send data through the socket to the server, EchoClient simply needs to write to the PrintWriter. To get the server's response, EchoClient reads from the BufferedReader. The rest of the program achieves this. If you are not yet familiar with the Java platform's I/O classes, you may wish to read Basic I/O.
The next interesting part of the program is the while loop. The loop reads a line at a time from the standard input stream and immediately sends it to the server by writing it to the PrintWriter connected to the socket
String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); }
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 end-of-input character. That is, EchoClient reads input from the user, sends it to the Echo server, gets a response from the server, and displays it, until it reaches the end-of-input. The while loop then terminates and the program continues, executing the next four lines of code
outer.close(); inner.close(); stdIn.close(); echoSocketSet.close();
These lines of code fall into the category of housekeeping. A well-behaved program always cleans up after itself, and this program is well-behaved. These statements close the readers and writers connected to the socket and to the standard input stream, and close the socket connection to the server. The order here is important. You should close any streams connected to a socket before you close the socket itself. This client program is straightforward and simple because the Echo server implements a simple protocol. The client sends text to the server, and the server echoes it back. When your client programs are talking to a more complicated server such as an HTTP server, your client program will also be more complicated. However, the basics are much the same as they are in this program
previous | Home | Next |