Java Tutorial - Java Script : Sockets

Java Tutorial - Java Script :

Sockets

When making a finger request, specify a username followed by an at sign (“@”) and a hostname, the same format as an email address. One famous real-life example is johnc@ idsoftware.com, the finger address of id Software founder John Carmack. You can request his .plan file by running the Finger application as follows:
java Finger johnc@idsoftware.com
If johnc has an account on the idsoftware.com finger server, the output of this program is his .plan file and perhaps other information (though it appears he’s no longer updating it). The server also lets you know whether a user can’t be found. Blues News includes addresses for other game designers who provide .plan updates such as id Software developer Timothee Besset (ttimo@idsoftware.com). The Finger application uses the StringTokenizer class to convert an address in user@host format into two String objects: user and host (lines 10–13). The following socket activities are taking place:
·         Lines 19–20—A new Socket is created using the hostname and port 79, the port traditionally reserved for finger services, and a timeout of 20 seconds is set.
·         Line 21—The socket is used to get an OutputStream, which feeds into a new PrintStream object.
·         Line 22—The finger protocol requires that the username be sent through the socket, followed by a carriage return (‘\015’) and linefeed (‘\012’). This is handled by calling the print() method of the new PrintStream.
·         Lines 23–24—After the username has been sent, an input stream must be created on the socket to receive input from the finger server. A BufferedReader stream, in, is created by combining several stream-creation expressions together. This stream is well suited for finger input because it can read a line of text at a time.
·         Lines 26–32—The program loops as lines are read from the buffered reader. The end of output from the server causes in.readLine() to return null, ending the loop.
The same techniques used to communicate with a finger server through a socket can be used to connect to other popular Internet services. You could turn it into a telnet or webreading client with a port change in line 19 and little other modification.