A client/server application model typically is viewed as a remotely located, high powered computing device that stores a large quantity of data with business logic to access them in a network. The user interface is handled by the client software on a relatively cheap machine. This idea is not distinct because any machine serving the request can potentially be called a server. Although the server waits for the client to start a conversation, in some cases the same program may act as both client and server. In that sense, a single machine can act as a network providing the communication between the client and the server program that goes through layers of a TCP/IP protocol stack. A socket is an API provided by the OS to realize the connection. The package java.net provides the necessary ingredients to implement the socket communication between two of the topmost TCP/IP layers: application and transport. The article elaborates the concept behind the client/server model with hands-on details in creating a TCP client/server application in Java.
A socket establishes the connecting endpoints between two hosts. The Socket class provided by Java is used for both clients and servers. The basic operations area is as follows:
The last three operations are specific to servers only; they are implemented by the ServerSocket class. The client program work flow occurs in the following manner:
The following code demonstrates how a client socket can be created. The application is a rudimentary chat application that enables one to chat with the server in full-duplex mode until the connection is explicitly closed.
The ServerSocket class provides the niche to write everything about servers in Java. The main job of a server socket is to wait for incoming calls and respond accordingly. ServerSocket runs on the server bounded by a port and listens to incoming TCP connections. When a client Socket attempts to connect to that port, the server wakes up to negotiate the connection by opening a Socket between two hosts. This Socket object is used to send data to the clients. The work flow of the server program can be defined as follows:
The following code demonstrates how a Server socket can be created. Observe that most of the functionality is very similar to the client program created earlier. The element that designates this class as the server is the ServerSocket object.
Here we have been discussing connection-oriented, stream-based transmission represented by TCP packets. There is another side of the story of client/server interaction which is connectionless, called a datagram, represented by UDP packets. More details will be provided when we create an UDP client/server application in the next article.
A hint: New Java IO (NIO.2) can be interestingly merged with Socket programming. These are advanced features. Here, we have covered the basic more so than in future articles.
As we have seen, creating client/server communication through sockets is not that difficult. Interested readers may focus on the ServerSocket class in particular, because this class is very powerful and can be leveraged to create your own customized server or reinvent an HTTP, FTP server. Intriguingly, creating your own HTTP server can be a great learning experience and it’s also not that difficult, at least in Java.