The sticky bag phenomenon in python

Source: Internet
Author: User

#黏包: The sender sends the data, and the receiving side does not know how to receive a Data chaos. #关于分包和黏包: #黏包: The sender sends two strings "Hello" and "word", and the receiver receives "Helloword" at one time #分包: send-side send string " Helloword ", the receiver receives two strings" Hello "and" word "#虽然socket环境有这些问题, but the TCP transmission data can guarantee a few points: #顺序不变, the sender sends Hello, the receiver receives hello in a bit order,    The characteristics of this TCP protocol have become the key to #解决分包, sticky packet problem. #分割的包中间不会插入其它数据 #注意: #如果用socket通讯, you must define a protocol, the current standard protocol is: message header (header) + message length + message body #tcp Protocol unpacking mechanism: #当发送缓冲区的长度大于网卡的MTU时, TC P splits the data sent into several packets to #MTU是Maximum the abbreviation for the transmission unit. means the largest packet transmitted over the network. The unit of MTU is byte # Most network devices have an MTU of 1500. If the MTU of this machine is larger than the MTU of the gateway, the large packets will be split to transmit #这样会产生很多数据包碎片, increase packet loss rate, and reduce network data. #面向字节流的通信特点和Nagle算法 #TCP是面向连接的, flow-oriented, Provide high-reliability services #收发两端 (client and server) have to have a pair of sockets, so the sender in order to send multiple packets to the receiving end of a more efficient #发到对方, using the Optimization method (Nagle algorithm), the small number of smaller data volume data to merge into a large block of data, Then the packet sends the #这样, the receiver side, the receiving end is difficult to distinguish, must provide the scientific unpacking mechanism. That is, the flow-oriented communication is a #注意 without boundary protection: #对于空消息: TCP is based on the data flow, so the message can not be empty, you need to add empty message processing mechanism in the program server and client, Prevents the program from #卡住, and UDP is datagram-based, and even if you enter empty content, it can be sent, and the UDP protocol will help you encapsulate the header of the message #发送过去 #可靠黏包的tcp协议: TCP                   The protocol data is not lost, the packet is not received, the next receive, will continue the last receive, the receiving end     #总是在收到ack时才会清除缓冲区内容. The data is reliable, but sticky.                # Causes of TCP sticky packets: #socket客户端 <<<<<<<< user->>>>>>socket server                                 #    ^|                ^|                                 #    ^|                ^|                                 #    ^|                ^| # cache <<<<<<<<<< Kernel state >>>>>>>>>>> Cache #socket数据传输过程中的            Description of the user and kernel states: #发送端可以是一K一K地发送数据, and the receiving side of the application can be two K two k to take the data # of course, it is possible to take 3 K or 6K data at a time, or only a few bytes of data at a time. # that is, the data the application sees is a whole, or a stream, # How many bytes of a message are not visible to the application, so the TCP protocol is a stream-oriented protocol, which is also the cause of the sticky-packet problem.            And UDP is a message-oriented protocol, each UDP segment is a message, # The application must extract the data in the message unit, can not fetch any byte of data at a time, this is very different from TCP. # How do I define a message?    You can think of the other one-time Write/send data is a message, # need to understand is when the other side send a message, no matter how the bottom fragment Shard # The TCP protocol layer will be the data segment that makes up the entire message is completed before it is rendered in the kernel buffer. #例如基于tcp的套接字客户端往服务端上传文件, sent when the file content is sent in accordance with a paragraph of the stream of bytes, # in the receiver looked at, do not know the file's byte streamWhere to start and where to end # In addition, the packets caused by the sender are caused by the TCP protocol itself, TCP to improve transmission efficiency, # The sender often collects enough data before sending a TCP segment. If you need to send data for a few times in a row, # usually TCP will send the data to a TCP segment based on the optimization algorithm once, so that the receiving party receives the sticky packet data. #UDP不会发生黏包现象: #UDP是无连接的, for datagrams, providing efficient service #不会使用块的合并优化算法, because UDP supports a one-to-many pattern, the Skbuff (socket buffer) # of the receiving end uses a chain structure to record each incoming UDP packet , in each UDP packet there is a message header (message source address, port and other information), # So, for the receiving end, it is easy to distinguish processing.    That is, message-oriented communication is a message-protected boundary. # for empty messages: TCP is based on data flow, so send and receive messages can not be empty, which requires the client and the server to add a null message processing mechanism #, to prevent the program stuck, and UDP is based on the datagram, even if you enter the empty content (direct carriage) can also be sent, # UDP protocol will help you encapsulate On the message hair sent over. Unreliable non-sticky UDP protocol: UDP recvfrom is blocked, # a recvfrom (x) must be the only one sendinto (y), after the X-byte data is completed, if the y;x data is lost, # This means that UDP is not sticky at all, but will lose data , not reliable.    #注意: #udp和tcp一次发送数据长度的限制 #用UDP协议发送时, the maximum amount of data sent with the SendTo function is: 65535-ip (20) –UDP Header (8) = 65507 bytes. # When sending data with the SendTo function, the function returns an error if the sending data is longer than this value. (Discard this package, do not send) # when sent with the TCP protocol, because TCP is the data flow protocol, there is no limit to the size of the packet (regardless of the size of the buffer), # This means that when using the Send function, the data length parameter is not restricted.    In fact, the specified data is not necessarily sent out one at a time, # If this data is longer, it will be sent in fragments, if relatively short, may wait and the next time the data sent together. # #数据链路层: #因为网卡的MTU一般被限制在1500 #数据链路层大小限制 1500-ip Baotou (-UDP) bagHeader (8) = 1472 # if sendto (num) # num > 65507 error # 1472< num < 65507 split at Data link layer #udp本身是不可靠协议, once unpacked, will result in data loss # NUM < 1472 is the ideal state

  

The sticky bag phenomenon in python

Related Article

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.