In general, the socket can be divided into TCP sockets and UDP sockets, further, can also be divided into server-side sockets and client sockets. In this section we first focus on the TCP socket server-side Socket,java in the ServerSocket class corresponding to it, this class is mainly used to create a socket service on the server side, establish a communication terminal, passively waiting for the client connection, once the data into the listening port, This class will be able to receive this data.
The ServerSocket class is located in the java.net package, and it has many methods, including creating a socket instance, binding a port, blocking a receive connection, and so on. The general server works in the following steps:
① builds a ServerSocket instance that is bound to listen on the specified local port so that the server socket can receive connections to the specified port.
The ②serversocket instance calls the Accept () method to block, gets the next client connection, and, if there is a client connection, creates a socket and returns.
③ handles the socket returned by the ②, typically using the InputStream object of the socket instance to communicate with the client with the OutputStream object.
After the ④ communication is complete, use the Close method of the socket to close the client socket connection.
The ⑤ Loop executes ② to ④, continuously receiving the next client connection.
How exactly does ServerSocket work? How does the bottom layer work? How does the operation of the application layer react to the bottom of the system? Figure 2-3-2-2 An attempt to illustrate how ServerSocket works. Divided by dashed lines, the upper layer is the application layer, the lower layer is the system bottom. The entire process can be divided into the following steps:
(1) using the ServerSocket constructor to create an ServerSocket instance, the incoming port number, which is the port number of the listener.
(2) The ServerSocket instance is then blocked, initializing the underlying socket and listening.
(3) Create socket underlying data structure, this socket initial state is closed.
(4) Fill in the port number passed in by the application layer and set the socket status to listening state.
(5) The server-side starts listening for client access.
(6) The client accesses three times after the handshake completes the connection, ready to receive the socket connection.
(7) Create a new socket data structure for the connection, set the remote port with the remote IP based on the incoming packet packets. Since the handshake was completed three times, the state is set to the connection establishment.
(8) The underlying socket data structure that establishes the connection is placed into a queue buffer, which is supplied with a layer read.
(9) After the ServerSocket instance calls the Accept () method, it starts polling the queue buffer above and, once a new connection is in the queue, creates and returns an application-tier socket instance immediately.
(10) This works, waiting for client access until the ServerSocket instance is closed.
Figure 2-3-2-2 ServerSocket Bottom Operating principle
As a result, creating a serversocket instance is actually creating a socket and declaring a possession of a port to the TCP/IP stack, while listening to this port, where the system socket caches all packets destined for this port into a region. And ServerSocket's Accept () method is to constantly get the cache area of the socket, in the application layer as each time there is a connection, you can get a socket.
Socket Series server-side Socket--serversocket class