tcp協議才會有粘包問題,udp協議沒有粘包問題
粘包問題的幾種情況:
d1 和d2間隔時間長,兩者資料量小,不會發生粘包問題
d1和d2間隔時間非常短,資料量小,會發生粘包問題
d2比較大,d1比較小,間隔時間也短,服務端一次性讀取d1和d2的一部分,出現了粘包問題
d1大,d2比較小,獲取d1的一部分,然後獲取了d1的剩餘部分和d2,出現了粘包問題
解決方案:
#tcp服務端.py
import socket
import struct
import subprocess
server = socket.socket(socket.af_inet,socket.sock_stream)
server.bind(('127.0.0.1',8000))
server.listen(5)
print('start...')
while true:
conn, client_address = server.accept()
print(client_address)
while true:
try:
cmd = conn.recv(1024)
print(cmd)
pipeline = subprocess.popen(cmd.decode('utf8'),
shell=true,
stderr=subprocess.pipe, stdout=subprocess.pipe)
stdout = pipeline.stdout.read()
stderr = pipeline.stderr.read()
count_len = len(stdout)+len(stderr)
guding_bytes = struct.pack('i',count_len)
conn.send(guding_bytes)
conn.send(stderr + stdout)
except connectionreseterror:
break
conn.close
#tcp客戶端.py
import struct
from socket import *
client = socket(af_inet,sock_stream)
client.connect(('127.0.0.1',8000))
while true:
cmd = 'dir'
client.send(cmd.encode('utf8'))
guding_bytes = client.recv(4)
count_len = struct.unpack('i',guding_bytes)[0]
data = client.recv(count_len)
print(data.decode('gbk'))
#udp服務端.py
import socket
server = socket.socket(socket.af_inet,socket.sock_dgram)
server.bind(('127.0.0.1',8000))
print('start...')
while true:
data, client_addr = server.recvfrom(1024)
print(client_addr)
print(data)
server.sendto(data.upper(),client_addr)
#udp客戶端.py
import socket
client = socket.socket(socket.af_inet,socket.sock_dgram)
while true:
msg = input('please enter your msg')
client.sendto(msg.encode('utf8'),('127.0.0.1',8000))
data = client.recvfrom(1024)
print(data)
#服務端.py
import socketserver
import subprocess
import struct
class myhandler(socketserver.baserequesthandler):
# 通訊迴圈
def handle(self):
while true:
try:
cmd = self.request.recv(1024)
print(cmd)
pipeline = subprocess.popen(cmd.decode('utf8'),
shell=true,
stderr=subprocess.pipe,
stdout=subprocess.pipe)
stdout = pipeline.stdout.read()
stderr = pipeline.stderr.read()
count_len = len(stdout) + len(stderr)
guding_bytes = struct.pack('i', count_len)
self.request.send(guding_bytes) # 4
self.request.send(stderr + stdout)
except connectionreseterror:
break
# 使用socketserver的連線迴圈(併發),但是使用了自己的通訊迴圈
# myhandler = myhandler()
if __name__ == '__main__':
s = socketserver.threadingtcpserver(('127.0.0.1', 8080), myhandler, bind_and_activate=true)
s.serve_forever()
#客戶端.py
import socket
client = socket.socket(socket.af_inet,socket.sock_stream)
client.connet(('127.0.0.1',8000))
while true:
msg = input(please enter your msg ).strip()
msg = 'client'
if len(msg) == 0: continue
client.send(msg.encode('utf8'))
data =client.recv(1024)
print(data)
#客戶端1.py
import socket
client = socket.socket(socket.af_inet,socket.sock_stream)
client.connet(('127.0.0.1',8000))
while true:
msg = input(please enter your msg ).strip()
msg = 'client'
if len(msg) == 0: continue
client.send(msg.encode('utf8'))
data =client.recv(1024)
print(data)
#客戶端2.py
import socket
client = socket.socket(socket.af_inet,socket.sock_stream)
client.connet(('127.0.0.1',8000))
while true:
msg = input(please enter your msg ).strip()
msg = 'client'
if len(msg) == 0: continue
client.send(msg.encode('utf8'))
data =client.recv(1024)
print(data)
06 27 html 標籤(文字 字元 列表)
h1 h6 1 6級標題 heading hr 水平標尺 水平線 horizontal ruler p 段落 paragraph sub sup sub 設定乙個下標,sup 上標,如 h2o em strong 表示強調的內容 em 語氣強調 斜體 strong 內容強調 粗體 今天天氣真好啊 d...
python網路程式設計 TCP網路程式設計
tcp程式設計 客戶端 import socket 1 套接字 tcp socket socket.socket socket.af inet,socket.sock stream 2 建立鏈結 tcp socket.connect 172.27.35.1 8080 3 傳送資訊 tcp socke...
網路程式設計 網路基礎
1.物理層 提供建立 維護和拆除物理鏈路所需的機械 電氣 功能和規程的特性 提供有關在傳輸介質上傳輸非結構的位流及物理鏈路故障檢測指示。在這一層,資料還沒有被組織,僅作為原始的位流或電氣電壓處理,單位是位元。常用協議 eia tia rs 232 eia tia rs 449 v.35 rj 45 ...