If the length of the package is Len, it needs to be sent by X times
len=len1+len2+. +lenx
In fact, it can be sent so that can be achieved
1) The content of the first direct hair
Baotou | Len
Body (LEN1)
2) second-time hair
Body (LEN2)
3) The first X
Body (Lenx)
Second, the procedure simple example divided 3 times the contract
TcpSend.cpp: Defines the entry point of the console application. #include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <time.h> #include <errno.h> #include <signal.h> #include <stdlib.h> #include <string.h> #include <unistd.h > #include <sys/wait.h> #include <sys/time.h> #include <netinet/in.h> #include <arpa/inet.h > #define IPSTR "172.16.40.252" #define PORT 9080#define BUFSIZE 1024int Main (int argc, char **argv) {int SOCKFD, ret, I, H;struct sockaddr_in Servaddr;char str1[4096], buf[bufsize], *str;char xmlbody[4096];char XmlBody1[4096];char xmlBody2 [4096];socklen_t len;fd_set t_set1;struct timeval tv;if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0) {printf ("Gen Network connection failed, this thread is about to terminate---socket error!\n "); exit (0);}; Bzero (&servaddr, sizeof (SERVADDR)), servaddr.sin_family = Af_inet;servaddr.sin_port = htons (port), if (Inet_pton ( Af_inet, Ipstr, &servaddr.sin_addr) <= 0) {printf ("Failed to create network connection, this thread is about to terminate--inet_pton error!\n"); exit (0);}; If (Connect (SOCKFD, (struct sockaddr *) &servaddr, sizeof (SERVADDR)) < 0) {printf ("Connection to server failed, connect error!\n"); exit (0);} printf ("Connection to Remote");//data to be sent sprintf (Xmlbody, "package sent three times") sprintf (xmlBody1, "package"); Strcat ( XmlBody2, "sent three times");//Send Data str= (char *) malloc (n); len = strlen (xmlbody); sprintf (str, "%d", Len); Memset (STR1, 0, 4096); Strcat (str1, "Post/test http/1.1\n"); Strcat (str1, "cache-control:no-cache\n"); Strcat (str1, "Pragma: No-cache\n "), strcat (str1," user-agent:java/1.6.0\n "), strcat (str1," host:172.16.40.252:8888\n "); Strcat (STR1," Accept:text/html, Image/gif, Image/jpeg, *; Q=.2, */*; Q=.2\n "); Strcat (str1," connection:keep-alive\n "); Strcat (str1," content-type:application/x-www-form-urlencoded\n " ); Strcat (str1, "content-length:"); strcat (str1, str); Strcat (str1, "\r\n\r\n");p rintf ("%s\n", str1); ret = Write (sockfd , Str1,strlen (STR1));//First time only Baotou, there will be no content ret = Write (Sockfd,xmlbody1,strlen (xmlBody1));//The contents of the second send package ret = write (SOCKFD, Xmlbody2,strlen (XmlBody2));//ThirdThe contents of the second post package if (Ret < 0) {printf ("Send failed! Error code is%d, error message is '%s ' \ n ', errno, Strerror (errno)); exit (0);} else{printf ("message sent successfully, total%d bytes sent!") \ n ", ret);} Fd_zero (&T_SET1); Fd_set (SOCKFD, &t_set1); while (1) {sleep (2); tv.tv_sec= 0;tv.tv_usec= 0;h= 0;printf ("--------------->1"); h= Select (Sockfd +1, &t_set1, NULL, NULL, &TV);p rintf ("--------------->2"),//if (H = = 0) continue;if (H < 0) {C Lose (SOCKFD);p rintf ("Select detects an exception while reading a data message, and the exception causes the thread to terminate!") \ n "); return-1;}; if (H > 0) {memset (buf, 0, 4096), i= read (SOCKFD, buf, 4095), if (i==0) {close (SOCKFD);p rintf ("When the data packet is read, the remote is closed and the thread terminates!") \ n "); return-1;} printf ("%s\n", BUF);}} Close (SOCKFD); return 0;}1) You can see the contents of the package as follows:
2) Note the contents of the package
<pre name= "code" class= "HTML" >content-length:len -----Here n must be the content length of the package, if the Len is less than the contents of the body of the package, Causes a few strings to be received by the server over the packet.
Post Request sub-package program