IV. Network Programming (TCP sticky package)

A. Sticky phenomenon package (TCP)

1. Causes of sticky bag

Data transmission protocol TCP 
tcp protocol unpacking mechanism 
when the transmission buffer is larger than the MTU end of the card, the data will be transmitted tcp split into several data packets sent. 
Maximum Transmission Unit MTU is an abbreviation of. It means the maximum packet transfer on the network. MTU is in bytes. Most network equipment MTU is 1500 . 
If the MTU is larger than the MTU native gateway, large data packets will be transmitted apart, it will generate a lot of packet fragmentation, loss rate increases, reducing the speed of the network
Stream-oriented communication characteristics Nagle algorithm and
 
TCP (transport control protocol, the Transmission Control Protocol) is connection-oriented and stream-oriented, high reliability service. 
The receiver and transmitter (client and server) must have eleven pairs of socket, therefore, in order to transmit end a plurality of packets addressed to the receiving end, other more efficient to send
, using the optimization method (the Nagle Algorithm), the times, at intervals smaller and smaller amount of data the data combined into a large block of data, then the packet. In this way, the receiving end, it is hard to tell the difference, and must provide scientific unpacking mechanism. I.e., non-oriented communication message stream is protected boundaries. For an empty message: tcp is based on the data stream, then transmitted and received message can not be empty, which requires handling mechanisms are added in the empty message client and server, a program to prevent jamming
, and is based on data reported udp, even your input is empty the contents (direct carriage return), can also be sent, udp protocol will help you package the message header sent in the past. Reliable sticky package tcp protocol: protocol tcp data will not be lost, did not receive complete package, the next reception, will continue to continue to receive the last, had always end will clear the buffer contents upon receipt ack. The data is reliable, but will stick package.

                                                         

 
udp is based on data reported, even if you enter an empty content (direct carriage return), it can also be sent, udp protocol will help you package the message header sent in the past. 
Not sticky unreliable packet udp protocol: udp the recvfrom is blocked, a recvfrom (x) must be a unique sendinto (y), received over x bytes of data even if completed, if Y; x data is lost, it udp means that will not stick package, 
but will lose the data, unreliable
python subprocess module is a module version 2.4 from the start of introduction.
Let us first make a tcp-based remote command execution of the program (the -l command LS; lllllll; pwd)
Import
subprocess # subprocess from python module is a module of version 2.4 introduced. # Mainly used to replace some of the old method of modules, such as os.system, os.spawn *, os.popen *, commands. * And the like. subprocess to execute external commands by child processes cmd = subprocess.Popen ( " dir " , shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE) Print (cmd.stderr.read (). decode ( " GBK " ) ) Print (cmd.stdout.read (). decode ( " GBK " ))

2. Based on the sticky tcp packet protocol implementation

server1
we first make a tcp-based remote command execution of the program (the -l command LS; lllllll; pwd) Import socket # Import subprocess Server = socket.socket () server.bind (( " 192.168.59.1 " , 8600 )) Server. the listen ( 5 ) conn, addr = server.accept () the while True: cmd = the iNPUT ( " Please enter your terminal command: " ) conn.send (cmd.encode ( " UTF-8 " )) RET = conn.recv ( 1024 ) Print (ret.decode ( " UTF-. 8
" ), " This is the server ha ha ha " ) conn.Close () server.close ()
client1
 we first make a tcp-based remote command execution of the program (the -l command LS; lllllll; pwd) 

Import socket Import subprocess Client = socket.socket () the client.connect (( " 192.168.59.1 " , 8600 )) the while True: msg = client.recv (1024 ) RET = subprocess.Popen (msg.decode ( ' GBK ' ), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE) ERR = ' I ERR ' + (RET. stderr.read ()). decode ( " GBK " ) OUT='我是out'+(ret.stderr.read()).decode("gbk") print(err) print(out) client.send(err.encode("utf-8")) client.send(out.encode("utf-8")) print(msg.decode("utf-8")) client.close()
server2

from socket import *
import subprocess
ip_port=('127.0.0.1',8888)
server=socket(AF_INET,SOCK_STREAM)
server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
server.bind(ip_port)
server.listen(5)
while True:
    conn,addr=server.accept()
    print('客户端',addr)
    while True:
        cmd=conn.recv(1024)
        if len(cmd) == 0:break
        res=subprocess.Popen(cmd.decode('utf-8'),shell=True,
                         stdout=subprocess.PIPE,
                         stdin=subprocess.PIPE,
                         stderr=subprocess.PIPE)
        stderr=res.stderr.read()
        stdout=res.stdout.read()
        conn.send(stderr)
        conn.send(stdout)
client1

import socket
ip_port=('127.0.0.1',8888)
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
res=s.connect_ex(ip_port)
while True:
    msg=input('请输入命令哈: ').strip()
    if len(msg) == 0:continue
    if msg == 'quit':break

    s.send(msg.encode('utf-8'))

    act_res=s.recv(1024)
    print(act_res.decode('utf-8'),end='')

Caching mechanism in the case of two receiver

The recipient is not timely received packet buffers, resulting in multiple packet receive (a piece of data sent by the client, the server received only a small portion of the server the next time or take the time to close the last remaining data from the buffer generating stick package) 

to sum up

Stick pack phenomenon occurs only in tcp protocol:

1. On the surface, the package sticky problem is because the cache mechanism features the sender and recipient, stream-oriented communication protocol TCP.

2. In fact, mainly because the receiver does not know the limits between the message, does not know how many bytes of data caused by the time extraction

 

 

Guess you like

Origin www.cnblogs.com/Sup-to/p/11135465.html