Basic socket Communication Flow

Source: Internet
Author: User
Server Common Model

1. Initialize the socket (for listening)

1) Initialization

SOCKET s;

s = Socket (...)

2) Binding

Bind (S, ... );

3) Monitoring

Listen (S, ... );

2. Establish a connection

1) Check the status

int ret = SELECT (...);

if (Ret > 0) {

New Connection

}

2) Establish a new connection (if a client requests a connection)

temp = accept (...); Note that you do not use the original socket at this time, but instead create a new socket (temp) to communicate with the client

3. Send and receive data

1) Detection of read-in data

int ret = SELECT (...)

if (Ret > 0) {

Have new data

}

2) Receive data

ret = recv (...);

3) Check sending data

int ret = SELECT (...);

4) Send data

ret = Send (...);

Client Common model

1. Initialization

1) Create socket

s = Socket (...)

2) Binding port

Bind (S, ...)   ; The client's IP address and port do not need to be fixed and can be automatically assigned by the system. So you can bind ports and protocols without using BIND.

2. Establish a connection

Connect (...)

3. Send and receive data

With the service side

4. Close the connection



From for notes (Wiz)

Basic socket Communication Flow

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.