March 5, 2021
Hot Topics:

Java Socket Programming in Client/Server Applications

  • By Sridhar M S
  • Send Email »
  • More Articles »

In client/server applications, the client and server components usually do not reside on the same computer (i.e., the client could be installed on a computer that is different from the one hosting the server installation); yet logically they are components of the same application. To enable the client and server components to communicate with each other over a network, systems often rely heavily on sockets.

Because Java provides the required capabilities for developing socket-based applications relatively easily and hides all the complexity involved, Java developers have an advantage in providing quick solutions to networking problems.

The ServerSocket and Socket Classes

Java supports socket development through the java.net package. In particular, ServerSocket and Socket are the socket-related classes. They provide infrastructure for server and client development, respectively. This article explores these classes—and how to use them—in more detail.

You use the ServerSocket class on the server component, where it listens for incoming requests. ServerSocket generally is bound to a port, always active, and non-blocking. This means that it immediately transfers or hands over any incoming request to a different component and then continues listening for new incoming requests.

To connect with the server component, the client component simply requests a connection to the computer where it expects the server to be running. It includes the computer name and the port to which the server is bound in this request. The resulting connection is permanent for the client; it will not be shared with any other instance of the client software running on the same machine.

Confused? Here is the concept in a nutshell:

  1. The server listens on the port to which it is bound and waits for incoming requests.
  2. When it receives a connection request, it creates a socket and associates the new connection to that socket.
  3. From then on, the client will communicate with the server via this newly created socket (although it is not aware of it).

Imagine achieving this setup using native code. It would be a lot of work, and if you weren't aware of the low-level constraints (like many developers), you would end up producing poor quality code. Luckily, the Java socket APIs are well tested and used by a large group of developers, which ensures that all possible hidden issues are caught and addressed. That is why developers using Java can produce high-quality client/server applications based on sockets.

Page 1 of 2

This article was originally published on September 23, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date