AIM: TO CREATE A TCP SOCKET FOR RECEIVEING DATA FROM CLIENT
The socket is the software abstraction used to represent the “terminals” of a connection between two machines. For a given connection, there’s a socket on each machine, and you can imagine a hypothetical “cable” running between the two machines with each end of the “cable” plugged into a socket. Of course, the physical hardware and cabling between machines is completely unknown. The whole point of the abstraction is that we don’t have to know more than is necessary.
In Java, you create a socket to make the connection to the other machine, then you get an InputStream and OutputStream (or, with the appropriate converters, Reader and Writer) from the socket in order to be able to treat the connection as an IO stream object. There are two stream-based socket classes:
A ServerSocket that a server uses to “listen” for incoming connections and
A Socket that a client uses in order to initiate a connection.
Once a client makes a socket connection, the ServerSocket returns
(via the accept( ) method) a corresponding server side Socket through which direct communications will take place.
From then on, you have a true Socket to Socket connection and you treat both ends the same way because they are the same. At this point, you use the methods getInputStream( ) and getOutputStream( ) to produce the corresponding InputStream and OutputStream objects from each Socket. These must be wrapped inside buffers and formatting classes just like any other stream object.
ServerSocket’s job is to wait until some other machine connects to it, then to return an actual Socket. This is why ServerSocket seems to be a bit misnamed, since its job isn’t really to be a socket but instead to make a Socket object when someone else connects to it.
However, the ServerSocket does create a physical “server” or listening socket on the host machine. This socket listens for incoming connections and then returns an “established” socket (with the local and remote endpoints defined) via the accept( ) method. The confusing part is that both of these sockets (listening and established) are associated with the same server socket. The listening socket can accept only new connection requests and not data packets.
So while ServerSocket doesn’t make much sense programmatically, it does “physically.” When you create a ServerSocket, you give it only a port number. You don’t have to give it an IP address because it’s already on the machine it represents.
When you create a Socket, however, you must give both the IP address and the port number where you’re trying to connect. (On the other hand, the Socket that comes back from ServerSocket.accept( ) already contains all this information.)
- Create the server socket and begin listening.
- Call the accept() method to get new connections.
- Create input and output streams for the returned socket.
- Conduct the conversation based on the agreed protocol.
- Close the client streams and socket.
- Go back to step 2 or continue to step 7.
- Close the server socket.
1. Create the client socket connection.
- Acquire read and write streams to the socket.
- Use the streams according to the server's protocol.
- Close the streams.
- Close the socket.