For networking applications beyond what the URL and URLConnection classes offer (for example, for other protocols or for more general networking applications), Java provides the Socket and ServerSocket classes as an abstraction of standard Transmission Control Protocol (TCP) socket programming techniques. The Socket class provides a client-side socket interface similar to standard UNIX sockets. Create a new instance of Socket to open a connection (where hostName is the host to connect to and portNumber is the port number):
Socket connection = new Socket(hostName, portNumber);
After you create a socket, set its timeout value, which determines how long the application waits for data to arrive. This is handled by calling the socket’s setSoTimeOut(int) method with the number of milliseconds to wait as the only argument: connection.setSoTimeOut(50000); By using this method, any effort to read data from the socket represented by connection waits for only 50,000 milliseconds (50 seconds). If the timeout is reached, an InterruptedIOException is thrown, which gives you an opportunity in a try-catch block to either close the socket or try to read from it again. If you don’t set a timeout in a program that uses sockets, it might hang indefinitely waiting for dataAfter the socket is open, you can use input and output streams to read and write from that socket:
BufferedInputStream bis = new
BufferedInputStream(connection.getInputStream());
DataInputStream in = new DataInputStream(bis);
BufferedOutputStream bos = new
BufferedOutputStream(connection.getOutputStream());
DataOutputStream out= new DataOutputStream(bos);
You don’t need names for all these objects; they are used only to create a stream or stream reader. For an efficient shortcut, combine several statements as in this example using a Socket object named sock:
DataInputStream in = new DataInputStream( new BufferedInputStream( sock.getInputStream()));
In this statement, the call to sock.getInputStream() returns an input stream associated with that socket. This stream is used to create a BufferedInputStream, and the buffered input stream is used to create a DataInputStream. The only variables you are left with are sock and in, the two objects needed as you receive data from the connection and close it afterward. The intermediate objects—a BufferedInputStream and an InputStream—are needed only once. After you’re finished with a socket, don’t forget to close it by calling the close() method. This also closes all the input and output streams you might have set up for that socket. For example:
connection.close();
Socket programming can be used for many services delivered using TCP/IP networking, including telnet, Simple Mail Transfer Protocol (SMTP) for incoming mail, Network News Transfer Protocol (NNTP) for Usenet news, and finger. The last of these, finger, is a protocol for asking a system about one of its users. By setting up a finger server, a system administrator enables an Internet-connected machine to answer requests for user information. Users can provide information about themselves by creating .plan files, which are sent to anyone who uses finger to find out more about them. Although it has fallen into disuse in recent years because of security concerns, finger was the most popular way that Internet users published facts about themselves and their activ- ities before weblogs were introduced. You could use finger on a friend’s account at another college to see whether that person was online and read the person’s current .plan file.