TCP and UDP Differences between

1,TCP Connection oriented protocol ,UDP Is a connectionless protocol ;

2,TCP Protocol transmission is reliable ,UDP Protocol transmission “ do everything in one's power ”;

3,TCP Flow control is possible ,UDP no way ;

4,TCP Can be segmented ,UDP no way ;

5,TCP Slow transmission speed , Large occupation of resources ;UDP Fast transmission speed , Small resource occupation .

What is connection oriented : Before formal data transmission , Pass the prepared agreement first , Establish a point-to-point connection , Then transfer the data .

 TCP Message format :( Back )

TCP --- Variable length head

seq Serial number ,ack Confirm serial number ( The lengths are 4 Bytes ) Used to ensure TCP Three handshakes and four waves

header length : Data offset ,4 position , The value of this field is TCP Head ( Include options ) Length divided by 4.

Pointer flag meaning :

URG: Emergency flag bit , Set 1 activation , Bring emergency data to the forefront , Corresponding emergency pointer , The emergency pointer indicates which bit the emergency data goes to , That is, the data before the pointer is the data that needs urgent processing ( General setting 0, Data transmission for emergency priority processing )

ACK: Confirmation mark bit , Used to determine the reply message , Linkage with confirmation serial number , Set 1 Confirm serial number activation when , The confirmation sequence number is different from the confirmation mark bit , Confirm that the serial number is ack.

PSH: Express Lane , Set 1 You don't have to queue up when , Indicates that data is being transmitted

RST: Data reset information identifier ( Used to close the connection abnormally , Forced shutdown , unwanted ack confirm )

SYN: Request flag bit , Request connection setup 1 .

FIN:TCP End identifier of the connection , Indicates the end of the connection .

Pseudo Header Check ---
Encapsulating the network layer 12 Verify the contents of bytes together :32 Bit source IP address ,32 target IP address ,8 Bit agreement number ,8 Bit reservation ,16 Bit message length , One TCP The smallest data transmission unit is 20 byte .


UDP Only source and destination port numbers and UDP Length and check core , So one UDP The minimum unit of data is 8 Bytes .

TCP and UDP The biggest difference between data transmission protocols is TCP Need to establish reliable connection oriented ( Three handshakes and four waves ).

TCP Three handshakes ( That is, it interacts by sending three data packets ):

First handshake :Client Flag bit SYN Set as 1, Randomly generate a value seq=X, And send the packet to Server,Client get into SYN_SENT state , wait for Server confirm . The Second Handshake :Server Flag bit after receiving data packet SYN=1 know Client Request to establish connection ,Server Flag bit SYN and ACK All set to 1, Confirm serial number activation ack=X+1, Because the serial number sent for the first time is X, So confirm the serial number ack=X+1, And randomly generate a value seq=Y, And send the packet to Client To confirm the connection request . Third handshake :Client After receiving the confirmation , inspect ack Is it X+1,ACK Is it 1, If correct, the flag bit will be ACK Set as 1,ack=Y+1, And send the packet to Server,Server inspect ack Is it Y+1,ACK Is it 1, If correct, the connection is established successfully , Complete three handshakes , subsequently Client And Server Data transfer can begin between .

TCP Four waves :

First wave :Client Send a FIN, Used to close Client reach Server Data transfer . Second wave :Server received FIN after , Send a ACK to Client, Confirm that the serial number is the received serial number +1( And SYN identical , One FIN Occupy a sequence number ). Third wave :Server Send a FIN, Used to close Server reach Client Data transfer . Fourth wave :Client received FIN after ,Client get into TIME_WAIT state , Then send a ACK to Server, Confirm that the serial number is the received serial number +1,Server get into CLOSED state , Complete four waves .

Why does it take three handshakes and four waves :
When the client sends a wave request , The data has been sent , The server still has data to send , Until the server sends the data , The server sends the last packet FIN, At this time, the client sends ACK information , The server will be closed . Data transmission is not required to establish a connection .

TCP Guarantee mechanism of transmission reliability : confirm , Retransmission , sort , Flow control ( Sliding window mechanism )

Network layer encapsulation :-IP address -IP Message format :(TCP and IP All variable length heads )

TTL: The packet is forwarded every time it passes through the router ,TTL The value will be reduced by one , When in a packet TTL Value is 0 Time , The router will no longer forward the packet , It will be discarded directly .

8 Bit protocol :TCP--6  UDP--17  ICMP--1

ip Slice :

MTU --- Maximum transmission unit --- 1500 byte ( Maximum length of a data segment , The largest transmission unit in the physical layer )
MSS --- Maximum segment length --- 1460(MSS Not included TCP and IP Protocol header length ) 

encapsulation : Right to left .

Unpacking : From left to right .