In a world which is wired, standalone applications are becoming obsolete. The facilitators for the connectivity of applications, at a low level, are sockets. Any language, whether high-level or low-level, must provide APIs to handle sockets if its creators want it to be widely accepted.
Java is no exception. Java facilitates socket programming through its java.net package. And true to its philosophy, it abstracts out most of the low-level "nitty-gritty" associated with sockets and provides a clean object-oriented API to work with.
In this article I will discuss how to use the java.net package to net enable any application (CLI or GUI based) with TCP based sockets. The first section will cover what sockets are and how they have been supported in Java. In the second section, I will enumerate the steps you need to take to use sockets within an application. In the last two sections I will develop a real world application using sockets. That sets the course for this discussion.
Sockets: what are they?
If one looks up the definition, the most common one would be "A socket is one endpoint of a two-way communication link between two programs running on the network." To put it differently, it is through sockets that applications access the network and transmit data. The types of sockets are as varied as the purposes and platforms of applications. There are three types of sockets:
Unix Domain Sockets
Internet Domain Sockets
NS Domain Sockets
Of these only Internet Domain Sockets are supported across all platforms. So to maintain the cross-platform characteristic intact, Java supports only Internet Domain Sockets. The next question that arises is what are the characteristics of an Internet Domain Socket and what protocols are supported by it? Here are the answers.
Internet Domain Sockets
By definition "An Internet socket (or commonly, a socket or network socket), is a communication end-point unique to a machine communicating on an Internet Protocol-based network, such as the Internet." All applications communicating through the Internet use a network socket. The feature that distinguishes a network sockets from other sockets is the protocols that it supports. The supported protocols are:
The difference between them is based on whether the protocol is connection oriented or not. Here are the details.
TCP is one of the core protocols of the Internet protocol suite. The protocol guarantees reliable and in-order (correct order of packets) delivery of data from sender to receiver. To put it simply, it's reliable. The second aspect of TCP is that it is connection oriented. That means TCP requires that a connection be made between the sender and receiver before data is sent. The socket associated with TCP is known as the Stream Socket.
UDP, like TCP, is one of the core protocols of the IP suite. However, unlike TCP, it neither guarantees in-order delivery of data nor does it requires a connection to be established for sending the data. To put it simply, UDP is an unreliable and connectionless protocol. Sockets associated with UDP are known as Datagram Sockets.
Raw IP is a non-formatted protocol, unlike TCP and UDP. It works at network and transport layers. A socket associated with Raw IP is known as a Raw Socket. UDP and TCP sockets just receive the payload or the data, whereas Raw Sockets receive the header info of the packet along with the data. The downside of Raw Sockets is that they are tightly coupled with the implementation provided by the underlying host operating system.
Next let's see how Java places the different types of sockets in its libraries.
Sockets in Java
Like all other functionalities provided by Java, functionalities to work with sockets are also "packaged" as a package and its classes. The following are the package and its main classes that help in accessing sockets:
Among the above, Java abstracts out most of the low-level aspects of socket programming. Here are the details.
The java.net package contains all the classes required to create network enabled applications. ServerSocket and Socket are also part of this package. Apart from these classes, it also contains classes to connect to the web server, create secured sockets, and so forth.
The ServerSocket class provides server sockets or sockets at server side. Such sockets wait for requests over the network. Once such requests arrive, a server socket performs operations based on the request and may return a result. The ServerSocket class wraps most of the options required to create server-side sockets.
The Socket class provides client-side sockets or simply sockets. They are at the client side connecting to the server, sending the request to the server and accepting the returned result. Just as ServerSocket exposes only the compulsory parameters required to create a server-side socket, similarly, Socket asks the user to provide only those parameters that are most necessary.
That covers sockets and Java. In the next section, I will discuss the steps involved in creating socket-based applications.