From the angle of grasping the packet to analyze TCP establishment three times handshake (several written interview, all have TCP three times handshake)

Source: Internet
Author: User
Tags htons

The three-time handshake process is as follows:


To build a network environment:

Use Commview to grab the package (because it can catch the loop back to the package), open it, and capture 8888-port packets.

The server-side program is:

#include <stdio.h> #include <winsock2.h>//Winsock interface #pragma comment (lib, "Ws2_32.lib")//Winsock implement int Mai  N () {WORD wversionrequested;         Double-byte, Winsock version of the library wsadata Wsadata; Winsock library version of the relevant information wversionrequested = Makeword (1, 1);

	0X0101 is: 257//Load Winsock library and determine the Winsock version, the system will fill in the Wsadata WSAStartup (wversionrequested, &wsadata);

	Af_inet represents the adoption of TCP/IP protocol family//Sock_stream, which is the usual default case unsigned int socksrv = socket (af_inet, sock_stream, 0);

	Sockaddr_in addrsrv; addrsrv.sin_family = af_inet; TCP/IP protocol family addrsrv.sin_addr. S_un. 
	S_ADDR = Inaddr_any; Addrsrv.sin_port = htons (8888);

	Socket corresponding to the port//bind the socket to an IP and port (IP identity host, port identification communication process) bind (Socksrv, (sockaddr*) &addrsrv, sizeof (SOCKADDR));

	Set the socket to listening mode, 5 indicates the maximum length of the queue waiting for the connection listen (Socksrv, 5);
	Sockaddr_in addrclient;
	int len = sizeof (SOCKADDR);
	
	unsigned int sockconn = Accept (Socksrv, (sockaddr*) &addrclient, &len);
		while (1) {char sendbuf[100] = {0}; scanf ("%s", sendBUF); Send (Sockconn, SendBuf, strlen (sendbuf) + 1, 0);
		Send data to client, the last parameter is generally set to 0 char recvbuf[100] = {0};
		Recv (Sockconn, recvbuf, 100, 0);
	
	printf ("%s\n", recvbuf);	
	} closesocket (Sockconn);
	Closesocket (SOCKSRV);
	
	WSACleanup ();
return 0; }

Start the server, there is no package at this time.


The client program is:

#include <winsock2.h>
#include <stdio.h>
#pragma comment (lib, "Ws2_32.lib")

int main ()
{
	WORD wversionrequested;
	Wsadata Wsadata;
	wversionrequested = Makeword (1, 1);
	
	WSAStartup (wversionrequested, &wsadata);

	Socket sockclient = socket (af_inet, sock_stream, 0);

	Sockaddr_in addrsrv;
	Addrsrv.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1");
	addrsrv.sin_family = af_inet;
	Addrsrv.sin_port = htons (8888);
	
	int ret = connect (sockclient, (sockaddr*) &addrsrv, sizeof (SOCKADDR));
	
	while (1)
	{
		char recvbuf[100] = {0};
		Recv (sockclient, recvbuf, 0);
		printf ("%s\n", recvbuf);

		Char sendbuf[100] = {0};
		scanf ("%s", sendbuf);
		Send (Sockclient, SendBuf, strlen (sendbuf) + 1, 0);

	}

	Closesocket (sockclient);
	WSACleanup ();

	return 0;
}
Open the client, at this point observation commview, there are three packets, this is the TCP established three times handshake.


In order to understand the TCP three times handshake, we can personally grasp the bag to analyze. The interviewer likes to ask this stuff the most.




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.