Python Full stack learning--day35 (sticky packet mechanism)

Source: Internet
Author: User
Tags unpack

Sticky bag

Sticky bag phenomenon

After executing multiple commands at the same time, the resulting results are likely to be only part of the result, and when other commands are executed, another part of the result of the previous execution is received, which is the sticky packet.

The implementation of sticky packets based on TCP protocol

Server Side

##### #server End from socket import *ip_port= (' 127.0.0.1 ', 8080) tcp_socket_server=socket (af_inet,sock_stream) tcp_ Socket_server.bind (Ip_port) Tcp_socket_server.listen (5) conn,addr=tcp_socket_server.accept () Data1=conn.recv (2) DATA2=CONN.RECV print ('-----> ', data1.decode (' Utf-8 ')) print ('-----> ', data2.decode (' Utf-8 ')) Conn.close ( ) Tcp_socket_server.close ()

Client Side

######### #client
#_ *_coding:utf-8_*_import socketbufsize=1024ip_port= (' 127.0.0.1 ', 8080) s=socket.socket (socket.af_inet,socket. SOCK_STREAM) res=s.connect_ex (ip_port) s.send (' Hello '. Encode (' Utf-8 ')) s.send (' egg '. Encode (' Utf-8 ')) S.close ()

Execution output:

Description: Medium is the effect of sticky bag

Principle

The cause of the sticky bag phenomenon

#你不知道在哪儿断句

Workaround:

When sending the data, first tell the other person to send the size of it.

At the time of sending, the size of the data sent first

When receiving, accept the size, receive the content according to the size

Automatic protocol

# ret = struct.pack (' i ', 1920000)
# Print (ret)
#
# ret = struct.unpack (' i ', ret)
# print (ret[0])

# ability to convert an arbitrary integer in the range into a fixed-length byte
# You can change it back #

# When sending the data
# XXXXTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
# Customizing a protocol

Import Structimport Socketsk = Socket.socket () sk.bind ((' 127.0.0.1 ', 9000)) Sk.listen () conn,addr = Sk.accept () while True :    ret = CONN.RECV (4)    length = Struct.unpack (' i ', ret) [0]    msg = conn.recv (length)    print (Msg.decode (' Utf-8 ')) Conn.close ()

  

Import Socketimport Structsk = Socket.socket () sk.connect ((' 127.0.0.1 ', 9000)) while True:    msg = ' Hello World '    Length = Struct.pack (' i ', Len (msg))    sk.send (length)    sk.send (Msg.encode (' Utf-8 ')) Sk.close ()

  

Packet-splitting mechanism of data transfer TCP protocol in the TCP protocol of Sticky Packet Genesis
When the length of the sending buffer is greater than the MTU of the NIC, TCP splits the sent data into a few data packets sent out. The MTU is an abbreviation for the maximum transmission unit. It means the largest packet transmitted over the network. The unit of the MTU is bytes. Most network devices have an MTU of 1500. If the MTU of this machine is larger than the MTU of the gateway, large packets will be removed and sent, which will result in a lot of packet fragmentation, increased packet loss rate and lower network speed.
Flow-oriented communication features and Nagle algorithm
TCP (Transport Control Protocol, transmission Protocol) is connection-oriented, stream-oriented and provides high reliability services. Both ends of the transceiver (client and server side) have one by one pairs of sockets, so the sending side in order to send multiple packets to the receiver, more efficient to the other side, the use of the optimization method (Nagle algorithm), the multiple interval small and small data volume data, combined into a large block of data, and then to the packet. In this way, the receiving end, it is difficult to distinguish out, must provide a scientific unpacking mechanism. That is, stream-oriented communication is a non-message-protected boundary. For the empty message: 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, The UDP protocol will help you encapsulate the message hair sent over. Reliable Packet TCP protocol: TCP protocol data will not be lost, no packet received, the next receive, will continue to receive the last time, the end will always receive an ACK to clear the buffer content. The data is reliable, but it will stick to the package.

Python Full stack learning--day35 (sticky packet mechanism)

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.