Single-threaded server model is the simplest of a server model, almost all of us programmers in the first contact with network programming (whether B/s structure or C/s structure) from this simple model. This model only provides simultaneous one client access, and multiple client access must wait until the end of the previous client access, one queue, that is, to provide a one-answer service.
Figure 2-6-1-1 Single-threaded blocking server model
Figure 2-6-1-1 shows how a single-threaded blocking server responds to client access. First, the server must initialize a ServerSocket instance, bind a port number, and make it listen to client access to provide a service. Then the client 1 remotely invokes the server's service, the server accepts the request, processes it, and returns information to client 1, the entire process is done within a thread. Finally, even if client 2 requests access before the server finishes processing client 1, it waits until the server responds to client 1 to respond to client 2.
We note that most of the socket operations are blocked, so-called blocking refers to the call does not immediately return the result of the call, and the current thread has been blocked, only if the call to obtain results or time out when the return. And our figure 2-6-1-1 also has a lot of nodes are blocked, for example, the server blocked listening client, until a client access to return a socket, for the client, after establishing a socket connection, also block until the server responds. Almost all IO operations are blocked, and the communication in the socket is reflected in network programming. This blocking to the network programming brings a problem, in black Block 1 and Black Block 2, when the server finished processing IO operation (where the IO operation is actually to send a message to the client), the server must wait until the client successfully received to continue to process the request of another client, during this period, The thread will not be able to perform any operations and respond to any client requests. And if the process is never sent to the client, the server will be stuck there and will not proceed with anything else.
This model is characterized by: The simplest server model, the whole process is only one thread, can only support the simultaneous processing of a client's request, if multiple client access must be queued, the server system resource consumption is small, but the concurrency capability is low, if encountering an IO operation error exception will cause the server to stop running, Poor fault-tolerant capability. Generally this model is generally used in access and concurrency, the request is short, stateless, the response time requirements are not high, processing logic more complex occasions.