Windows下编程抓包分析TCP的三次握手和四次挥手

最近在看TCP相关的书籍,写了个程序实现TCP的链接和建立,并抓包分析三次握手和四次挥手的过程。

程序代码如下:

#include 
#include "stdio.h"
#pragma  comment(lib,"ws2_32.lib")


int main(int argc, char* argv[])
{
	WORD sockVersion = MAKEWORD(2, 2);//版本号
	WSADATA data;    //用来保存WSAStartup调用后返回的windows Sockets数据
	if (WSAStartup(sockVersion, &data) != 0)
	{
		return 0;
	}

	sockaddr_in serAddr;
	serAddr.sin_family = AF_INET;
	serAddr.sin_port = htons(443);								//服务端端口
	serAddr.sin_addr.S_un.S_addr = inet_addr("10.19.166.150");	//服务端地址

	
	{
		SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if (sclient == INVALID_SOCKET)
		{
			printf("invalid socket !");
			return 0;
		}
		if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
		{
			printf("connect error !");
			closesocket(sclient);
			return 0;
		}
		closesocket(sclient);
	}
	WSACleanup();
	return 0;
}

抓到的包如下:

https://download.csdn.net/download/luchengtao11/12529764

你可能感兴趣的:(计算机网络)