Can One Socket Write and Read at the Same Time
Socket class and and so, how the client can transport data to and receive information from the server through the socket. The instance program implements a client, EchoClient, that connects to the Echo server. The Echo server simply receives data from its client and echoes information technology dorsum. The Echo server is a well-known service that clients can rendezvous with on port 7.
EchoClient creates a socket thereby getting a connection to the Repeat server. Information technology reads input from the user on the standard input stream, and then frontwards 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 customer program reads and displays the data passed back to it from the server:
import java.io.*; import java.net.*; public class EchoClient { public static void master(Cord[] args) throws IOException { Socket echoSocket = null; PrintWriter out = zero; BufferedReader in = null; effort { echoSocket = new Socket("taranis", 7); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: taranis."); System.leave(1); } grab (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: taranis."); System.exit(1); } BufferedReader stdIn = new BufferedReader( new InputStreamReader(Arrangement.in)); Cord userInput; while ((userInput = stdIn.readLine()) != nothing) { out.println(userInput); Organisation.out.println("repeat: " + in.readLine()); } out.shut(); in.close(); stdIn.close(); echoSocket.close(); } } Annotation that EchoClient 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 three statements in the try block of the main method are critical. These lines establish the socket connection betwixt the customer and the server and open up a PrintWriter and a BufferedReader on the socket:
echoSocket = new Socket("taranis", seven); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream())); The first statement in this sequence creates a new Socket object and names information technology echoSocket. The Socket constructor used here requires the name of the auto and the port number to which you lot desire to connect. The example program uses the host name taranis. This is the name of a hypothetical machine on our local network. When you lot type in and run this program on your machine, change the host proper name to the proper name of a car on your network. Make sure that the name you use is the fully qualified IP proper name of the machine to which you want to connect. The second statement is the port number. Port number 7 is the port on which the Echo server listens. The second statement gets the socket'southward output stream and opens a PrintWriter on it. Similarly, the 3rd statement gets the socket'due south input stream and opens a BufferedReader on it. The case uses readers and writers and so that information technology tin can write Unicode characters over the socket.
To transport data through the socket to the server, EchoClient merely needs to write to the PrintWriter. To get the server'due south response, EchoClient reads from the BufferedReader. The residual of the program achieves this. If you are not nevertheless familiar with the Coffee platform's I/O classes, you may wish to read I/O: Reading and Writing (just no 'rithmetic).
The adjacent 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 continued to the socket:
Cord 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 dorsum to EchoClient. When readline returns, EchoClient prints the information to the standard output. The while loop continues until the user types an stop-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 terminate-of-input. The while loop then terminates and the program continues, executing the next four lines of lawmaking:
These lines of lawmaking autumn into the category of housekeeping. A well-behaved programme always cleans up subsequently 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 whatever streams continued to a socket before you close the socket itself.out.close(); in.close(); stdIn.close(); echoSocket.close();
This customer plan 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 customer programs are talking to a more complicated server such as an HTTP server, your client program volition also exist more complicated. Withal, the nuts are much the same as they are in this program:
- Open up a socket.
- Open an input stream and output stream to the socket.
- Read from and write to the stream according to the server's protocol.
- Close the streams.
- Close the socket.
Source: http://www.iitk.ac.in/esc101/05Aug/tutorial/networking/sockets/readingWriting.html
0 Response to "Can One Socket Write and Read at the Same Time"
إرسال تعليق