First, stick package Why is there?
1 smaller seed consecutive points (the Nagle algorithm packaged transmitted) it will stick package
2. When the seed is greater than the received data transmission limit is transmitted '> recv bytes received the number, acceptance finish time, then when the second input command occurs not taken the first time to take complete data has been taken to complete the first data, which is also called stick package
in prior to solve stick package, we introduce the concept of a buffer zone:
What is the buffer:?
. when we receive and send receive and transmit buffer exists so what their role is,
the role of buffer zones:
1. temporarily store some data
2.1 buffer If your network fluctuations, ensuring stable transceiver, uniform data, however: it resulted in one stick pack phenomenon: when the seed consecutive multiple occurrences of data (small data), your data will stick sent together (the underlying reason is Nagle algorithm: the times, at intervals smaller and smaller amount of data the data combined into a large block of data, then the packet) on FIG.
This is the buffer !!!!!
So how to solve the stick package:
ideas provided: 1. set the server to send 10000 bytes
When the client received the data reception cycle, (1024) Example 1024 bytes every reception, until all of the bytes have been received together with the last decoded
Encounter problems
1.recv number can not be determined
2. Before you send data always give me a total data length Example: 1000 bytes, then the total data transmission
3. Client: accept a length of 10000 bytes and is recycled I long as it receives data I have been less than 10,000 bytes received
4. Second problem: the total data length is not fixedYou want to total_size int type is converted into bytes types can send
387 ----> str (387) '387' ----> bytes b'387 'length 3bytes
4185 ----> str (4185) '4185' ----> bytes b'4185 'length 4bytes
18,000 ------------------------------------------------- -----> length 5bytes
We have to solve:
The fixed length not converted to type int bytes of fixed length and may also be flipped back.
struct module
Then we can solve the stick package: This is a low version
import socket
import struct
import subprocess
phone = socket.socket()
phone.bind(('127.0.01',8878))
Python.listen(5)
while 1:
conn,adds = phone.accept()
while 1:
try:
receives_commands = conn.recv(1024)
receives_commands = receives_commands.decode('utf-8')
if receives_commands = 'Q':break
obj = subprocess.Popen(receives_commands,
shall = True
stdout = subprocess.PIPE
stderr =subprocess.PIPE )
ret = obj.stdout.read()+obj.stderr.read()
ret = ret.encode('utf-8')
ret_len = len(ret)
ret_len_struct = struct.pack('i',ret_len)
conn.send(ret_len_struct)
conn.send(ret)
except ConnectionResetError:
print('客户端终端')
break
conn.cloce()
phone.cloce()
#客户端
import socket
import struct
while 1:
dispatch_orders = inport('请输入您的命令:').strip().encode('utf-8')
if not dispatch_orders:print('不为空')
phone.send(dispatch_orders)
if dispatch_orders.upper() == b'Q':break
receives_commands_hand = phone.recv(4)
receives_commands_hand = struct.unpack('i',receives_commands_hand)
tata =b''
while len(tata)<receives_commands_hand:
tata+=phone.recv(1024)
tata = tata.decode('gbk')
print(f'来自服务端的回执 \n {tata}')
phone.close()
So that we solved the problem of a part of the stick package, then there is low version, there are also big on the version, we are here to provide the next idea:
- We want to make a fixed header
- You now have two types of bytes is not fixed length, we have to fix header, so
- You do not get a fixed-length header
- Struct module by using the fixed-length header is not fixed four bytes
- 4 byte sent first retransmission header
- First transmission header, and then send the total data (there are three data transmission process (the length of the first transmitter head 1., 2. and then the first transmitter, then send the total file data 3))