Sockets Programming in Java

A socket, in network terminology, is an end-point for communication between two processes on the network. In Java, sockets are represented by objects for exchanging data. The java.net package provides useful classes and interfaces for communication using sockets. Note that a socket is not a connection, it is an endpoint of communication. Sockets objects are also necessary for communication even if there is no connection (e.g. UDP). A connection (usually used for TCP), however, may be represented by the two end points.

1. Types of Socket

The Java Socket API is built on the top of the underlying transport layer protocol of TCP/IP protocol suit. In TCP/IP there are two transport layer protocols: Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). Accordingly, Socket API is categorized into two types. The Socket API that uses TCP as its underlying transport layer protocol is known as TCP Socket (or stream socket) and that which uses UDP is called UDP Socket (or datagram socket). The TCP delivers the data reliably in the same order as they were sent and is known as ‘connection-oriented’ protocol. On the other hand, UDP does not give any such guarantee on ordering and hence is known as ‘connectionless’ protocol. Java provides a rich set of classes and interfaces in java.net package to support both TCP and UDP sockets. The classes ServerSocket and Socket (together with other classes) are used to support Transmission Control Protocol (TCP) and provide connection- oriented uni-cast communication. To support User Datagram Protocol (UDP), DatagramSocket and MuiticastSocket classes are used that provide connectionless uni-cast, multi-cast and broadcast communication respectively. So, depending on the application requirement, any one/both of these two types of sockets may be used.

In this chapter, we shall discuss both types of sockets with several programming examples. A side-by-side comparison between these two types of sockets will be done later in this chapter.

2. Ports

Both TCP and UDP sockets use a fundamental concept called communication ports (or simply ports). We know that an IP address, which is assigned to a network interface card, indentifies the card hence the computer uniquely within a network. What happens if we want to refer to a particular process among many processes running in a computer? Specifying only the IP address of the computer is not enough. The concept of port number is introduced to resolve this problem. Ports are essentially 16-bit positive integers assigned to processes to distinguish one from another within the computer.

3. Socket Address

The TCP/IP protocol suite allows us to identify a machine uniquely all over the world using a 32-bit IP address. Many processes may run on a single computer. As mentioned in the previous section, each of these processes is assigned a locally unique positive integer called the port number. Any process can now be identified uniquely all over the world by using this port number together with the IP address of the machine it is running on. This IP address and port number combination can be thought of as an address called the socket address. A process willing to send data to another process needs to know only the socket address of the receiver process.

The socket address (i.e. IP address and port number) completes destination address. This means, data first reaches a specific destination IP address (i.e. a computer) and is further routed to the specific process having the destination port number. This way socket address makes the communication between two processes possible. The exact way of communications is illustrated with the help of numerous examples in the following sections.

The same port number may be used for different IP addresses for communication. Since, IP addresses are different, socket addresses are also different and hence processes having those socket addresses are uniquely identifiable.

4. Socket Address and Java

Usually the notion of socket address is explained with the context of a process. It is said that a process is assigned a locally unique port number. Actually in Java socket programming, port numbers are not assigned to processes; they are assigned to socket objects instead. These socket objects are created in the processes. Three types of socket objects (serversocket, DatagramSocket and MuiticastSocket) may be created. A process may logically create many socket objects [Figure 13.4:]. Each of these socket objects will have a port number bound to it. Consequently, each socket object has a socket address. This implies that a process does not basically have any socket address; rather socket addresses are assigned to socket objects. Usually, one process creates one socket object. That is why it is sometimes said that a process has a socket address.

Anyway, whatever be the case, it does not disturb Inter-Process Communication (IPC). Actually communication takes place between socket objects and since processes create them, processes can still communicate through these socket objects [Figure 13.4:].

Figure 13.4: Relation between socket objects and processes in Java

5. Reserved Ports

It was mentioned earlier that each process is assigned a locally unique integer. Ideally, any available (not assigned yet) integer may be used. However, remember that the client must know this port number to make communication possible. If port numbers are assigned arbitrarily, the client processes will not have any idea about them. That is why standard server processes are assigned some fixed port numbers agreed upon at design time. These port numbers are said to be reserved for those well-known processes. According to Internet Assigned Numbers Authority (IANA), port numbers from 0 to 1023 are reserved. You should not use these port numbers in your own network programs. However, if you want to implement standard processes (such as HTTP, FTP etc), you should ideally use these reserved ports (80 for HTTP, 20 for FTP) so that others can use them easily. Some reserved port numbers and their corresponding processes are mentioned in Table 13.1:

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *