Read/write of TCP for network programming sockets

Source: Internet
Author: User
Tags printf sendmsg socket

The successful return of a write call from a TCP socket simply means that we can reuse the original application process buffer and does not represent that the peer TCP or application process has received the data.


The peer TCP must acknowledge the received data, along with the constant arrival of the ACK from the peer, where TCP can discard the confirmed data from the socket send buffer, and TCP must keep a copy of the sent data until it is confirmed to the peer.


UDP does not save a copy of the app's process data so there is no need for a real send buffer, and the write call successfully returns the output queue that represents the datagram or all of its shards that have been added to the data link layer.


For a read call (the socket flag is blocked), if the receive buffer has 20 bytes, the request reads 100 bytes and returns 20. For a write call (the socket flag is blocked), if the request writes 100 bytes and the send buffer has only 20 bytes of idle space, then write blocks until all 100 bytes are given to the send buffer to return, if the socket flag in write is non-blocking, It returns 20 directly, so we can implement our own READN and writen functions.

Each TCP socket has a send buffer and a receive buffer, and each UDP socket has a receive buffer;


First, the service-side blocking is simulated:

Client

[CPP]  View Plain  copy   struct sockaddr_in serveradd;      bzero (& Serveradd, sizeof (Serveradd));   serveradd.sin_family = af_inet;   SERVERADD.SIN_ADDR.S_ADDR&NBSP;=&NBSP;INET_ADDR (SERV_ADDR);   serveradd.sin_port = htons ( Serv_port);      connfd = socket (af_inet, sock_stream, 0);      Int connresult = connect (connfd,  (struct sockaddr *) &AMP;SERVERADD,  sizeof (Serveradd));   if  (connresult < 0)  {        printf ("Connection failed, errno = %d\n", errno);       close (CONNFD);        return ;  }   else   {        printf ("Connection succeeded \ n");  }      ssize_t writelen;      char sendmsg[246988] = {0};      int count = 0;   while   (1)    {       count++;       if  (count == 5)  {           exit (0);       }       writelen = write (Connfd, sendmsg,  sizeof (sendmsg));       if  (writelen < 0)  {            printf ("Send failed \ n");            close (CONNFD);           return  ;       }       else        {           printf ("send successfully \ n");        }          }  


Server

[CPP] view plain copy int main (int argc, const char * argv[]) {struct sockaddr_in serveradd;

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.