A TCP packet structure is as follows:
A TCP packet consists mainly of the TCP header and the data part, the Baotou fixed part is 20 bytes, and the options and data parts are set to 4N (n can be 0) bytes according to the actual situation.
1.16bit Source port and destination port number, it can confirm the direction of data transmission (temporarily do not consider the lower package)
A 2.32bit ordinal, which is the part of the data portion of the TCP packet that is numbered. Assuming that the data to be sent is 100M, due to MSS (Maximum Segment Size maximum message length) limit, a TCP packet is not possible to transmit the data of 100M, so the data needs to be split, in order to ensure that the data after the split transfer can be correctly stitched on the receiving side, You need to number each split packet for transmission. Thus, this 32-bit ordinal refers to the first byte of the data portion of this package, which is the number of bytes in this 100M data. For example: Assuming that the first packet is sent, first take out the previous 1024 bytes of the 100M data sent, then this packet 32-bit ordinal is 1, and then takes the next 1024 bytes transmission, this time the first byte of the data portion is the 1025th byte of this 100M data, So the 32-bit serial number in this second TCP packet should be 1025. When the serial number exceeds 2^32, a cycle is carried out, counting back from 0 onwards.
3.32bit confirms the ordinal, similar to the above 32-bit ordinal, except that it refers to the number of the data portion of the next package that is expected to be received.
4.4bit The first ministerial degree, the unit is 4 bytes, refers to a TCP packet to remove the length of the data part, that is, the length of the Baotou fixed part + option part, 2^4-1 = 15, 15*4 byte = 60 bytes, that is, the Baotou fixed portion is 20 bytes, the option can be up to 40 bytes.
5. Identification bits:
URG: .....
Ack:tcp the answer bit of the package
The data in the PSH:TCP package needs to be passed to the application layer as soon as possible, rather than buffering the data until the buffer is full and then delivered to the application layer.
Rst:..........
Synchronization bit for SYN:TCP package
FIN: Indicates that the data transfer is complete and the connection is released.
TCP three-time handshake process:
Test code, Server side:
1#include <stdio.h>2#include <string.h>3#include <sys/types.h>4#include <sys/socket.h>5#include <netinet/inch.h>6 7 intMain (intargcChar*argv[])8 { 9 intRet,rn;Ten intSOCKETFD,ACFD; One intSocklen; A Charbuf[1024x768]; - - structsockaddr_in hostaddr; the structsockaddr_in clientaddr; - -SOCKETFD = socket (af_inet, Sock_stream,0); - if(Socketfd <0 ) + { -Perror ("Socket"); + return-1; A } at -Memset ((void*) &hostaddr,0,sizeof(HOSTADDR)); -hostaddr.sin_family =af_inet; -Hostaddr.sin_port = htons (6666); -HOSTADDR.SIN_ADDR.S_ADDR =htonl (inaddr_any); - inret = bind (SOCKETFD, (structSOCKADDR *) &hostaddr,sizeof(HOSTADDR)); - if(Ret <0 ) to { +Perror ("Bind"); - Close (SOCKETFD); the return-1; * } $ Panax NotoginsengRET = Listen (SOCKETFD,5); - if(Ret <0 ) the { +Perror ("Listen"); A Close (SOCKETFD); the return-1; + } - $Socklen =sizeof(structsockaddr); $ACFD = Accept (SOCKETFD, (structSOCKADDR *) &clientaddr, &Socklen); - if(Acfd <0 ) - { thePerror ("Accept"); - Close (SOCKETFD);Wuyi return-1; the } - Wu while(1) - { Aboutmemset (BUF,0x0,sizeof(BUF)); $RN = Read (ACFD, buf,sizeof(BUF)); - if(Rn <0 ) - { -Perror ("Read"); A Continue; + } theprintf"%s\n", buf); - } $ Close (SOCKETFD); the return 0; the}View Code
Client side:
1#include <stdio.h>2#include <string.h>3#include <unistd.h>4#include <sys/types.h>5#include <sys/socket.h>6#include <netinet/inch.h>7 8 #defineRequre "Hi,can You see me?"9 Ten intMain (intargcChar*argv[]) One { A intret; - intSOCKETFD,ACFD; - intSocklen; the structsockaddr_in hostaddr; - -SOCKETFD = socket (af_inet, Sock_stream,0); - if(Socketfd <0 ) + { -Perror ("Socket"); + return-1; A } at -Memset ((void*) &hostaddr,0,sizeof(HOSTADDR)); -hostaddr.sin_family =af_inet; -Hostaddr.sin_port = htons (6666); -HOSTADDR.SIN_ADDR.S_ADDR =htonl (inaddr_any); - inSocklen =sizeof(structsockaddr); -ACFD = Connect (SOCKETFD, (structSOCKADDR *) &hostaddr,sizeof(HOSTADDR)); to if(Acfd <0 ) + { -Perror ("Connect"); the Close (SOCKETFD); * return-1; $ }Panax Notoginseng - while(1) the { +SleepTen); AWrite (SOCKETFD, Requre,sizeof(requre)); the } + Close (SOCKETFD); - return 0; $}View Code
This is the CS-to-one connection method, a round to receive. First run the server side, under the Accept next line breakpoint, and then run the client side, in the next line of Connet breakpoint, use Wireshark grab packet, filter port 6666 TCP packet tcp.port = = 6666 , run, and then stop at the breakpoint, you can see that there are 3 of packages caught:
This is the 3 packets of the TCP three handshake, and the TCP handshake occurs on the client side when connect. for the sequence number and acknowledgment number of the handshake package I understand the sequence of three handshake.
Handshake for the first time:
Client: "Server, I'm looking for you!" (SYN = = 1), this is the first time I have spoken to you (Seq num = = 0), the TCP packet is as follows:
You can see the source port:33335,destination port:6666, which shows that the c--->s in this direction, sequence for 0,syn, no data part.
Second handshake:
Server: "Client I heard you call me (ACK = = 1), did you hear my response (SYN = = 1)? , this is the first time I have spoken to you (Seq num = = 0), I wait for you to speak to me for the second time (Ack num = = 1), the TCP packet is as follows:
You can see the source port:6666,destination port:33335, which shows that the s--->c in this direction, Sequence num for 0,ack num is set to 1,syn,ack, no data part.
Third handshake:
Clinet: "Server, Know you heard me (ack = = 1), we can chat, this is the second time I talk to you (Seq num = = 1), I wait for you and I speak for the second time (ack num = = 1)"
You can see the source port:33335,destination port:6666, which shows that the c--->s in this direction, Sequence num for 1,ack num is set to 1,ack, no data part.
TCP three-time handshake and data transmission analysis