Socket-series socket server-how to communicate with the client, socket server
The above sections have introduced the working steps of ServerSocket and Socket respectively, and analyzed their operating principles from the application layer to the bottom layer of the system. We have understood their respective parts. Now we will combine them, let's take a look at how they communicate and discuss in detail the details of their communication.
With the help of Figure 2-3-2-4, imagine that you are on a computer in the university classroom. You think that the teacher is very good at teaching and you don't have to listen to it, as A result, all of you open your web browser and surf your Web server. If you use browser A, the whole process is as follows:
① The browser confirms the target IP address and port number (http uses port 80 by default). Of course, if you enter a port address in the address bar of the browser, the target port number will be the port you specified, in addition, the browser also organizes http packets and transmits these parameters to the underlying socket of the system.
② System layer socket's operations on the TCP/IP protocol have been learned from the previous steps that layer by layer is actually to package packets layer by layer, so here we first organize http packets into TCP packets, then it is organized into IP packets, where the socket source port is randomly allocated (a random number greater than 1023), and finally placed in the TCP/IP stack to send messages in queue.
③ ServerSocket is blocking the listening and receiving connection, and the client socket is connected to it.
④ The server creates a socket based on the client request. This socket can be used to communicate with the client socket.
⑤ The Server Processes related logic, such as database operations and logical judgment.
⑥ After the server completes processing, it will generate an http packet for the client browser to display. The http packet is packaged through the tcp and IP protocols and returned to the client socket.
7. The client layer-by-layer resolution transmits the http packet to the browser, and the browser parses the http packet. the last beautiful and neat web page is displayed in front of you.
Release and close the connection.
If your Ingress accesses at the same time, the server will handle the other one in order to have better performance and respond to you at almost the same time, rather than processing the other one at the end. The server will adopt multi-thread processing, A thread processes a request.
Figure 2-3-2-4 ServerSocket and Socket Communication Model
The above communication model describes the communication between sockets from a macro perspective. In the communication process, data packets are transmitted, and the underlying data transmission model of the socket is learned. Figure 2-3-2-5 shows how data is transmitted from the application layer to the underlying system layer to the Internet. First, we will introduce two FIFO queues: SendQ and RecvQ. They are all at the bottom of the system and act as a buffer during transmission. SendQ sends the application layer to the underlying data of the system for buffering, and then queues are sent to the Internet queue, recvQ caches the data received from the Internet and waits for the application layer to read the data in the queue. The procedure is as follows:
① The Socket or Serversocket at the application layer writes the data to the SendQ queue at the bottom of the system.
② The SendQ queue sends the data to the Internet based on the FIFO principle to reach the target host. (From the transport layer, we can think that the data at this time is a TCP packet)
③ If data is sent from the Internet, it is first buffered by the received RecvQ queue.
④ The Socket or Serversocket at the application layer cyclically tries to read the RecvQ queue. If there is data, it reads it to the application layer.
The TCP protocol transfers data from SendQ to RecvQ sequentially. From the perspective of our application layer, there is no way to control or directly observe this transfer process. At the same time, TCP provides reliable data transmission services, so any data written to the SendQ queue must be kept in a copy until the other end of the connection receives the data successfully. In addition, writing data through the Socket output to SendQ does not mean that the data has been sent. They only go to the buffer zone, even if the Socket output stream is flush () the operation cannot ensure that the data is sent to the channel immediately.
Figure 2-3-2-5 socket Data Transmission Model
So can we assume that data is written at one end of the connection and can be read at the other end immediately? Next, we will further discuss how data in the SendQ and RecvQ queues is transmitted. This process should be said to be transmitted by blocks, the size of these blocks is independent from the buffer size of socket read/write on the application layer to a certain extent.
2-3-2-6. If the application layer socket calls the write () method of the output stream to write data to the SendQ queue, the three write data sizes are 1000 bytes, 2000 bytes, and 5000 bytes, respectively, assume that the size of the cache block in the SendQ queue is 3000 bytes. The size of the cache block in the first 1000 bytes plus 2000 bytes is exactly equal to 3000 bytes. Then the data in the two batches is transmitted as a block. The request arrives at the RecvQ queue over the Internet. The application layer socket then reads the data in RecvQ through the read () method of the input stream. If the input stream buffer reads 3000 bytes of the RecvQ queue, the returned value is 3000.
Figure 2-3-2-6 block transfer at the underlying layer of the socket
In actual use, the SendQ and RecvQ queues may be infinitely filled. To prevent a TCP connection from consuming all the system memory, you must limit the size of the SendQ and RecvQ queues. Once the number of RecvQ queues reaches the maximum value, TCP throttling will notify SendQ to stop sending data. When the data in my RecvQ is read by the socket input stream, it will pass it to me. In this way, the excessive sending can be effectively controlled to prevent situations that exceed the system's receiving and processing capabilities. For the socket output stream, data can be constantly written until the SendQ queue is filled up, And the write () method will block the wait.