我们知道, 如果服务端的IP不可达, 那么客户端在进行连接的时候, syn包收不到服务端的ack, 此时会进行超时重传。 如果服务端的IP可达, 但没有监听对应的端口时候, 会出现什么情况呢? 我们来看看。
客户端程序为:
#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("10.100.70.140");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(8765);
connect(sockClient, ( const struct sockaddr *)&addrSrv, sizeof(struct sockaddr_in));
#define N 20000
char szSendBuf[N] = {0};
for(unsigned int i = 0; i < N; i++) //字符数组最后一个字符不要求是‘\0’
{
szSendBuf[i] = 'a';
}
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;
}
客户端和10.100.70.140能ping通, 但10.100.70.140并没有监听8765端口, 实际抓包如下:
19:00:13.139662 IP 10.100.70.139.28393 > 10.100.70.140.ultraseek-http: Flags [S], seq 954253747, win 14280, options [mss 1428,sackOK,TS val 1557687529 ecr 0,nop,wscale 8], length 0
0x0000: 4500 003c ad5a 4000 4006 eb82 0a64 468b E..<.Z@.@....dF.
0x0010: 0a64 468c 6ee9 223d 38e0 c1b3 0000 0000 .dF.n."=8.......
0x0020: a002 37c8 a20d 0000 0204 0594 0402 080a ..7.............
0x0030: 5cd8 6ce9 0000 0000 0103 0308 0000 0000 \.l.............
0x0040: 0000 0000 0000 0000 0000 0000 ............
19:00:13.139828 IP 10.100.70.140.ultraseek-http > 10.100.70.139.28393: Flags [R.], seq 0, ack 954253748, win 0, length 0
0x0000: 4500 0028 9d88 4000 4006 fb68 0a64 468c E..(..@.@..h.dF.
0x0010: 0a64 468b 223d 6ee9 0000 0000 38e0 c1b4 .dF."=n.....8...
0x0020: 5014 0000 8236 0000 0101 080a 5cd8 0149 P....6......\..I
0x0030: 5cd8 0632 0000 0000 \..2....
可以看到, 当客户端发出syn后, 服务端立即给RST, 告诉客户端: 别瞎来, 我根本没有监听8765, 不可连接。
实际上, 如果要验证这样情况下的RST, 完全不用写程序, 可以直接telnet测试搞起。
好, 先说这么多, 后续我们将会谈到更多与RST有关的场景。