TCP / UDP details

# ## TCP / UDP protocol:  

```
TCP (Transmission Control Protocol) connection-oriented, reliable transport layer protocol (for example: call)
Advantages: reliable, stable, stable transmission complete, the data size is not limited
Disadvantages: slow, low efficiency, high system resources, pitch-catch need each other to confirm
Application: Web browser, e-mail, file transfer, large data transfers scene

UDP (User Datagram Protocol) is a connectionless and unreliable transport layer protocol (for example: text messaging)
Advantages: fast, people can chat at the same time, less resource consuming, without establishing a connection
Disadvantages: unstable and can not guarantee that every data can be received
Applications: IP telephony, real-time video conferencing, chat software, a small amount of data transmission scenarios

The client and server when the connection is established: three-way handshake
The client and server when disconnected: Four waving
SYN create a connection
ACK acknowledgment
FIN Disconnect
```

# ## three-way handshake: 

! [ 1559165147948] (D: \ gongxiang8 \ day25 \ Assets \ 1559165147948 .png)

# ## Four wave:

```
MSL survival time was the largest segment
Predetermined default MSL 2 minutes, but the practical application usually from 30 seconds, 1 minute and 2 minutes, etc..
```

! [ 1559165234479] (D: \ gongxiang8 \ day25 \ Assets \ 1559165234479 .png)

# ## overall thumbnail 

[! 1559171471044] (D: \ gongxiang8 \ day25 \ Assets \ 1559171471044 .png)

### socket 

```
Significance of the socket: pass network communication process, information splicing tools (Chinese: sockets)
# Development, only one port for a program to take effect in the test, allows the port to repeat bundle (deleted when developing) 
# add this sentence before the bind method, you can make a port reuse 
sk.setsockopt (socket.SOL_SOCKET , socket.SO_REUSEADDR, 1 )
```

![1555456389523](assets/1555456389523.png)

# ## stick pack

```
# Tcp protocol when sending data packets will be sticky phenomenon.     
    (1) Data package is sticky because the client / server will have a data buffer,
    Buffer for storing data temporarily, in order to guarantee that the complete received data, so the buffer will be set relatively large.
    ( 2 ) send and receive data at frequent, since borderless tcp transmission message length is not clear how much should be taken
    Cause the client / server, are likely to be a plurality of data as data capture, resulting in sticky package
```

# ## sticky package appears in both cases

```
# Sticky package phenomenon: 
    the transmitting side, since the two short data transmission interval time short, the packets are formed at the transmitting end sticky
 # sticky packet phenomena: 
    at the receiving end, since the two data are almost simultaneously transmitted to a counterpart cache, it is formed at the receiving end a sticky packet
 # summary: 
    the transmitting side, the short time interval between packets or a receiver, receiving is not timely, it will stick package
    Because the core is taken borderless tcp data, transmitted in the order determined not
```

# ## sticky package comparison: tcp and udp

```
# TCP protocol: 
drawback: when receiving no data between boundary, there may be several adhesion data into a resulting package sticky
Advantages: the data packet size is not limited, stable transmission packet loss

# UDP protocol: 
Advantages: the boundary between the data receiving time, transmission speed, not sticky bag
Disadvantages: packet size limitations (bandwidth is affected by factors such as a router), unstable transmission, packet loss may

# Tcp and udp packet is for unpacking and unpacking can be, in theory, no matter how much can be sent several times 
but failed to send tcp once, the other party does not respond (no other receipt), you can choose tcp recurrence, until response corresponding to its conclusion
And once failed to send udp, ask if the other party is not responsive, if the data is too large, easy to packet loss
```

# ## package to solve the sticky problem

```
# Resolve sticky package scenarios: 
    scenarios in real-time communication, need to read the message sent is what
 # is no need to resolve sticky package scene: 
    download or upload a file, and finally put packages together, sticky bag does not matter.
```

# ## module socketserver

```
# Most underlying network protocol is the socket, the socket module based on the original, and a layer of the package, it is SocketServer 
SocketServer to achieve tcp protocol, server side concurrency.
```

 

Guess you like

Origin www.cnblogs.com/max404/p/11837848.html