Detailed explanation of java socket communication based on bio
Bio, that is, blocking io. During socket based message communication, the socket server provides services to the outside, and the socket client can establish a connection to the socket server, send request data, and wait for the socket server to process, And return the processing result (response). For bio based communication, the socket server will block, that is, each time it accepts a socket connection to a client during listening, it will process the request, while other connected clients can only block and wait. It can be seen that in this mode, the processing capacity of the socket server is very limited, and the client can only wait until the server is idle The request is processed when.
Implementation of bio communication
The following is a simple logic for socket server to communicate with socket client based on bio mode. I have a perceptual understanding of this communication mode. The specific logic is described as follows: 1. The socket client connects to the socket server and sends the data "I am the client n."; 2. The socket server listens to the service port and receives the client request data. If the request data starts with "I am the client", respond to the client "I am the server, and you are the nth client.";
The socket server is implemented. The code is as follows:
The above implementation does not carry out complex exception handling.
Socket client implementation, the code is as follows:
First start the socket server process simplebiotcpserver, and then run the socket client simplebiotcpclient. It can be seen that the server receives the request data and then responds to the client, and the client receives the response data from the server.
In the above implementation, both the socket client and the server are written and read out at one time. In practice, if the amount of data is particularly large in each communication process, the server cannot accept it. You can read and process it in a loop when the number of data bytes requested by the client is determined.
In addition, if the stream is not wrapped in the above implementation, there will be performance loss in practice, such as inability to buffer.
For the data received by the socket server, it is much more convenient if the byte data read repeatedly can be stored through a variable length byte buffer. However, bytearrayoutputstream is used, for example:
Bio communication test
Let's test the processing efficiency of the socket server in the scenario of a large number of requests.
The first method: start 5000 socket clients through the for loop and send requests. The code is as follows:
After testing, it takes about 9864ms, about 10s.
The second method: start 5000 independent client threads and request at the same time, and the server counts:
After testing, it takes about 7110ms, about 7S, without much improvement.
Bio communication improvement
Through the above tests, we can find that blocking occurs when the socket server processes requests from the client, which seriously affects the efficiency of concurrent processing of requests. In fact, within the scope of the socket server's ability to receive connections from the client, the receiving requests can be independent, so that the above problems can be solved by processing the requests one by one thread. In this way, the server is a multi-processing thread, corresponding to the multi request of the client, and the processing efficiency is improved to a certain extent.
Next, receive the request through a single thread, and then delegate the thread pool to process the request concurrently through multiple threads:
It can be seen that this improved method enhances the concurrency of the server processing requests, but each request needs to be processed by one thread. A large number of requests cause the server to start a large number of processes for processing, which also occupies the server resources.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.