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 *) &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;