TCP/UDP connection and communication process

Source: Internet
Author: User
Tags htons

The TCP-based socket programming server program process is as follows:

1. Create a socket

Socketsocksrv = socket (af_inet, sock_stream, 0 );

 

2. Bind the socket to the local address and port

Sockaddr_inaddrsrv;

Addrsrv. sin_addr.s_un.s_addr = htonl (inaddr_any); // converts a u_long value to a network byte sequence.

Addrsrv. sin_family = af_inet;

Addrsrv. sin_port = htons (6000 );

 

BIND (socksrv, (sockaddr *) & addrsrv, sizeof (sockaddr); // bind the created socket

 

3. Set the created socket to the listening mode and wait for receiving client requests.

Listen (socksrv, 5 );

 

4. Wait for the customer's request to arrive. When the request arrives, it receives the connection request and returns a new socket for the connection.

Socketsockconn = accept (socksrv, (sockaddr *) & addrclient, & Len );

 

5. Use the returned socket to communicate with the client

Send (sockconn, szsendbuf, lstrlen (szsendbuf) + 1, 0 );

Recv (sockconn, szrecvbuf, 100, 0 );

 

6. Return and wait for another customer's request

Closesocket (sockconn );

 

7. Disable socket

 

 

The TCP-based socket programming client process is as follows:

1. Create a socket

Socketsockclient = socket (af_inet, sock_stream, 0 );

 

2. Send a connection request to the server

Sockaddr_inaddrsrv;

Addrsrv. sin_addr.s_un.s_addr = inet_addr ("192.168.0.1 ");

Addrsrv. sin_family = af_inet;

Addrsrv. sin_port = htons (6000 );

 

Connect (sockclient, (sockaddr *) & addrsrv, sizeof (sockaddr ));

 

3. Communicate with the server

Recv (sockclient, szrecvbuf, 100, 0 );

Send (...)

 

4. Disable socket

 

 

The UDP-based socket programming receiver sequence process is as follows:

1. Create a socket

Socketsocksrv = socket (af_inet, sock_dgram, 0 );

 

2. Bind the socket to the local address and port

Sockaddr_inaddrsrv;

Addrsrv. sin_family = af_inet;

Addrsrv. sin_port = htons (6000 );

Addrsrv. sin_addr.s_un.s_addr = htonl (inaddr_any );

 

BIND (socksrv, (sockaddr *) & addrsrv, sizeof (sockaddr ));

 

3. Waiting for receiving data

Sockaddr_inaddrclient;

Int Len = sizeof (sockaddr );

Tchar szrecvbuf [100];

Recvfrom (socksrv, szrecvbuf, lstrlen (szrecvbuf) + 1, 0, (sockaddr *) & addrclient, & Len );

 

4. Disable socket

 

The client sequence for UDP-based socket programming is as follows:

1. Create a socket

Socketsockclient = socket (af_inet, sock_dgram, 0 );

 

2. send data to the server

Sockaddr_inaddrsrv;

Addrsrv. sin_addr.s_un.s_addr = inet_addr ("192.168.0.1 ");

Addrsrv. sin_family = af_inet;

Addrsrv. sin_port = htons (6000 );

 

Sendto (sockclient, text ("hello"), lstrlen (text ("hello") + 1, 0, (sockaddr *) & addrsrv, sizeof (sockaddr ));

 

3. Disable socket

 

 

About the number of bytes sent by Recv and send:

The send (sockconn, szfilebuffer, nlength, 0) function sends nlenth bytes. If nlength is greater than the szfilebuffer size, the extra bytes are supplemented with \ 0. If it is smaller than the szfilebuffer size, the szfilebuffer string is sent to intercept nlength bytes.

 

The Recv (sockconn, szfilebuffer, nlength, 0) function principle is the same as above, except that when nlength is greater than the length of szfilebuffer, It is not filled with \ 0.

At the same time, the Recv function will send and receive the same \ 0.

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.