Java Socket Working mechanism
Sockets are an abstraction that describes the completion of communication between computers. There are many kinds of sockets, in most cases we use TCP/IP-based streaming sockets, which is a stable communication protocol.
Host A's application communicates with Host B's application and must establish a connection through the socket, and the socket connection must be established by the underlying TCP/IP to establish the TCP connection. Establishing a TCP connection requires the underlying IP to address the hosts in the network. The IP used by the network layer can help us to locate the target host based on the IP address, which is then specified by the TCP or UDP address, which is the port number. This makes it possible to use a single socket instance to uniquely represent the communication link for an application on a host.
Establish a communication link
When the client wants to communicate with the server, the client first creates a socket instance, the operating system assigns the socket instance a local port number that is not used, and creates a socket data structure that contains the local address, the remote address, and the port number. This data structure will remain in the system until the link is closed. Before the constructor for the socket instance is returned correctly, the 3-time handshake protocol for TCP will be made, and after the TCP handshake protocol completes, the socket instance object will be created, or a IOException error will be thrown.
Working with Java I/o 2