C + + socket, blocking mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.