我想從一個Python服務器發送一個長字符串到C#客戶端。該字符串長230400個字節。我都是以64字節爲單位發送和接收的。服務器代碼:Python到C#TCP傳輸損壞超過1523rd字節的數據
import socket
def initialize():
global s
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 1719))
s.listen()
initialize()
while(1):
sock, addr = s.accept()
msgstr = generate_msg_string() # irrelevant
msglen = len(msgstr)
totalsent = 0
while totalsent < msglen:
sent = sock.send(msgstr[totalsent:totalsent+64])
totalsent = totasent + sent
sock.close()
客戶端代碼:
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(ip, 1719);
byte[] ba = new byte[230400];
byte[] buffer = new byte[64];
tcpClient.ReceiveBufferSize = 64
int i=0;
while(i != 230400)
{
stream.Read(buffer, 0, 64);
buffer.CopyTo(ba, i);
i += 64;
}
tcpClient.Close();
我連續查了幾個連接 - 第1523個字節是正確的,其他都是廢話 - 至少看似隨意。
任何想法可能是什麼原因?
這確實有幫助。我收到的字符串現在完全正確。謝謝! – Lasooch