先看connect函数:
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
int main()
{
int sockClient = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addrSrv;
addrSrv.sin_addr.s_addr = inet_addr("1.2.3.4");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(8765);
connect(sockClient, ( const struct sockaddr *)&addrSrv, sizeof(struct sockaddr_in));
char szSendBuf[1024 * 64 * 10] = {0};
int iRet = send(sockClient, szSendBuf, sizeof(szSendBuf) , 0);
printf("send size is %d, iRet is %d\n", sizeof(szSendBuf), iRet);
getchar();
close(sockClient);
return 0;
}
运行并抓包:
xxxxxx$ sudo tcpdump -iany port 8765 -nlps0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
23:58:48.918381 IP 10.100.70.139.48767 > 1.2.3.4.ultraseek-http: Flags [S], seq 1851659651, win 14280, options [mss 1428,sackOK,TS val 1238166474 ecr 0,nop,wscale 8], length 0
23:58:49.916564 IP 10.100.70.139.48767 > 1.2.3.4.ultraseek-http: Flags [S], seq 1851659651, win 14280, options [mss 1428,sackOK,TS val 1238166724 ecr 0,nop,wscale 8], length 0
23:58:51.920561 IP 10.100.70.139.48767 > 1.2.3.4.ultraseek-http: Flags [S], seq 1851659651, win 14280, options [mss 1428,sackOK,TS val 1238167225 ecr 0,nop,wscale 8], length 0
23:58:55.932564 IP 10.100.70.139.48767 > 1.2.3.4.ultraseek-http: Flags [S], seq 1851659651, win 14280, options [mss 1428,sackOK,TS val 1238168228 ecr 0,nop,wscale 8], length 0
过一会儿, connect函数超时, 从抓包看, 连接的时候, 重传时间间隔分别为1, 2, 4秒, 这就是之前介绍过的karn算法。