Distributed Computing in Java 9
上QQ阅读APP看书,第一时间看更新

Sockets and streams

In distributed computing, network communication is one of the essential parts of any system, and the socket is the endpoint of every instance of network communication. In Java communication, it is the most critical and basic object involved. You may have seen browsers connect and communicate with the server through URLs over an HTTP protocol. Now, this is a high-level connection mechanism for accessing resources over the internet using URL and URLConnection. However, when huge amounts of data need to be exchanged in a secure way, then this method is not as performant as low-level data exchange. The reason is that every URL works on character streams, whereas low-level data exchange supports raw binary data exchange. Also, character streams are easy to hack and they pose security threats as the data is readable; you have to deploy heavy security frameworks to maintain the security standards. Sockets cater to low-level communication through which you can exchange binary data with other machines; the best example of this is either a client-server application or web server/app server communication.

Next to sockets, we have streams. It is through streams that data is passed in a defined sequence over the network. In this section, we will go through both sockets and streams in detail.

A socket is a handle that a local program can pass to the networking API to connect to another machine. It can be defined as the terminal of a communication link through which two programs/processes/threads running on the network can communicate with each other. The TCP layer can easily identify the application location and access information through the port number assigned to the respective sockets.

Any application running on a particular operating system can rely on the socket API supported by the respective operating system; this ensures seamless access and control over network socket programming. Across the internet, the socket programming API is, in general, Berkeley-sockets-standard-compliant. As per the standards defined by Berkeley sockets, sockets are nothing but a way of defining the description of a file (or can be treated as a handle to the file). Operations across sockets and files are observed to be quite similar (read/write/open/close). But, in general, they are treated quite differently, and people prefer to use interfaces, such as send and receive, on a socket. An internet socket is characterized by the following:

  • A local socket address containing the IP address and port number
  • A transport protocol, for example, TCP, UDP, and raw IP; this means that TCP port 53 and UDP port 53 are distinct sockets

During an instance of communication, a client program creates a socket at its end and tries to connect it to the socket on the server. When the connection is made, the server creates a socket at its end and then server and client communication is established.

The java.net package provides classes to facilitate the functionalities required for networking. The socket class programmed through Java using this package has the capacity of being independent of the platform of execution; also, it abstracts the calls specific to the operating system on which it is invoked from other Java interfaces. The ServerSocket class offers to observe connection invocations, and it accepts such invocations from different clients through another socket. High-level wrapper classes, such as URLConnection and URLEncoder, are more appropriate. If you want to establish a connection to the Web using a URL, then note that these classes will use the socket internally.