This describes the blocking mode of the socket below:
First, the socket has several functions
Socket () Create socket
Bind () binds the socket to an address
Listen () Turn on Soket Monitor
Accept () accepts others to connect me, this is a blocking function, the general server call.
Recv () accept the message from the other side, this is also blocking function, because TCP to wait for the other party to complete the message after the execution of the message from the buff copy. This client and server are to be called, note ~!!!!!
Send () sends a message
Connect () connects to the socket, which is typically the client.
The above accept and recv are blocking functions, pay attention to!!!
Example:
1, the server opens, will block in the ACCETP function, waiting for others to connect.
2. Client 1 turns on connect and then returns immediately to continue executing the client code. At this point, the server receives the request, continues to execute the server code, the general connection succeeds after writes the RECV function, therefore the server blocks in the recv.
3. Client
Time Client 1 Client 2 server
1 Accept, block
2 Connect and return immediately
3 Con with Client 1 received
4 performing recv, blocking
5 Connect
6 (Received client 2 connection, but the program is blocked, so queued)
7 Send ("C2")
8 (Received client 2 data, but the program is blocked, so queued)
9 recv (), blocking
Send ("C1"), and recv () block
11 Receive Client 1 data c1 and send ("Hello C1, im Server")
12 Received data ("Hello C1, im Server") Communication complete
After the server finishes 11 steps, immediately look at the queue there is a not processing, so processing C2 connection, received data "C2", Sent ("" "Hello C2, im Server")
14 Received data ("" "Hello C2, im Server") Communication complete
Accept () block
C + + socket, blocking mode