Java.net: from The Java Programming Language

This is an example of socket programming in java taken verbatim from The Java Programming Language by Arnold, Gosling and Holmes (Addison-Wesley, 3-ird ed.).

Documents

Overview

Java provides the programmer with Objects which abstract the behavoir of software constructs. The construct of concern here is the socket. The socket is an end-point of communication. It is an invention of BSD Unix, one of their solutions to network communication.

Whatever a socket it is, a pair of them forms a communication channel across the internet, or even locally between two applications on the same machine. One socket is a server socket. It is created and does a passive listen on a port on the local machine. The other socket is a client socket. It is created and does an active connect, giving the machine name and port number where awaits a listening server socket. If there is a listening socket at the machine and port pair requested, a connection is established.

                     <=============== 
     client-socket                       server-socket (machine name, port)
                      ===============>
Through this connection two streams of bytes can flow, one from client to server the other from server to client. Bytes are put into the stream by a write to the socket, and removed from the stream by a read from the socket.

The two most important classes of the java.net package which provides the socket API to the Java programmer are (I quote from the API):