C-language socket send () data cache problem

Source: Internet
Author: User
Tags usleep htons

Send ()functions are used by defaultNaglealgorithm. NagleThe algorithm passes unacknowledged data into a buffer until it accumulates to a certain number of methods that are sent together. To reduce the number of fragmented small packets that the host sends. So supposeSend ()when the function sends the data too fast, the algorithm will send some data together and send it out uniformly. Assuming this is not the case, the receiving end will encounter seemingly strange problems, such as successRecv ()the number and success ofSend ()are not equal in number of times. In this case, the receiving end canRecv ()is the return value of0to infer whether the sending end is sent.

throughSetSockOpt ()of theTcp_nodelayoption to disableNaglealgorithm. But the experiment does not seem to be effective, just for the same number ofSend (), set theTcp_nodelayoption afterRecv ()the number of successes is several times more than before the setup. This is not a description set upTcp_nodelayafter that the system will go into maximum effort not to cache. But supposeSendis too fast, it will still be cached.

so. Suppose you don't want the data for Send () to be cached locally until a certain number is sent, but send () How many times it is sent. The safe way is to call the Usleep () function after each send , giving the system a response time.

below The sample demonstrates the send () The speed of the call to the data whether the packaging effect, staring at Server usleep (

Tcp_nodelay is the only option to use the IPPROTO_TCP layer, and the macro tcp_nodelay header file is linux/tcp.h or netinet/ Tcp.h.

I debug a day's program because I do not know the problem of the Send () data cache. My Wuyi Labor Day!!

server.c :

#include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/types.h> #include < sys/socket.h> #include <sys/wait.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/ inet.h> #include <netinet/tcp.h>//#include <linux/tcp.h>int main () {int SOCKSV, sockcl;struct sockaddr_ In Server_addr;struct sockaddr_in client_addr;int sin_size;if ((SOCKSV = socket (af_inet, sock_stream, 0)) = =-1) {printf (" Sever socket fail\n "); return-1;} memset (&server_addr, 0, sizeof (struct sockaddr_in)); server_addr.sin_family = Af_inet;server_addr.sin_port = htons (6001); server_addr.sin_addr.s_addr = Inaddr_any;int r = 1;setsockopt (SOCKSV, Sol_socket, so_reuseaddr, &r, sizeof ( INT.); int t = 1;if ( -1 = = setsockopt (SOCKSV, Ipproto_tcp, Tcp_nodelay, &t, sizeof (int))) {printf ("setsockopt fail\n"); return-1;} if (bind (SOCKSV, struct sockaddr *) &server_addr,sizeof (struct sockaddr)) = =-1) {printf ("Server bind fail\n"); return-1;} if (Listen (SOCKSV, 5) = = -1) {printf ("Server Listen fail\n"); return-1;} while (1) {sin_size = sizeof (struct sockaddr_in), if (Sockcl = Accept (SOCKSV, (struct sockaddr *) &client_addr, & sin_size)) = =-1) {printf ("Server Accept fail\n"); continue;} int times = 1024;int Allbytes = 0;int i;for (i = 0; I < times; i++) {char buf[] = "#this is a message from Ptypeserver"; NT Sendbytes;if ((sendbytes = Send (Sockcl, buf, strlen (BUF), 0)) = =-1) {printf ("Server send Fail\n");} Usleep (+); Allbytes + = sendbytes;} printf ("has send%d packages to client, allbytes=%d\n", Times, Allbytes); close (SOCKCL);}}


client.c :

#include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/types.h> #include < sys/socket.h> #include <sys/wait.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/  Inet.h>int Main () {int Socksv;char buf[1024 * 1024];IF ((SOCKSV = socket (af_inet, sock_stream, 0)) = =-1) {printf ("socket Fail\n "); return-1;} struct sockaddr_in server_addr;memset (&server_addr, 0, sizeof (struct sockaddr_in)); server_addr.sin_family = AF_ Inet;server_addr.sin_port = htons (6001); server_addr.sin_addr.s_addr = inet_addr ("192.168.1.100"); if (Connect (SOCKSV , (struct sockaddr *) &server_addr, sizeof (struct sockaddr)) = =-1) {printf ("Connect fail\n"); return-1;}  int times = 0;int Allbytes = 0;int numbytes = 1;while (numbytes! = 0) {times++;if (numbytes = recv (SOCKSV, buf, sizeof (BUF), 0) = =-1) {printf ("recv fail\n"); return-1;} Buf[numbytes] = ' + ';//printf ("numbytes=%d buf=[%s]\n", numbytes, buf); Allbytes + = numbytes;} printf ("Server is closed, with recv%d pAckages from server, "allbytes=%d\n", Times-1, allbytes);} 

READ: HTTP://BAIKE.BAIDU.COM/LINK?URL=-QGA0U7IV5TNO-QNORYKDMNAZOEODCGK-PKIVFCOY-N6VHOITKDZLCG1VZYJQJ1DNOLPAAA54E7HRQQX6BC_E_



C-language socket send () data cache problem

Related Article

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.