我想從一個C#程序(服務器)運行在PC上,使用套接字技術發送兩個整數到Python客戶端腳本(它運行在Linux機器上)。直到現在我能夠發送字符串。C#發送整數到python
服務器:
TcpListener listener = new TcpListener(IPAddress.Any, 12345);
Invoke(new PrintMsg(printMsg), new object[] { "Trying to connect" });
listener.Start();
for(; ; ;)
{
TcpClient client = null;
try
{
client = listener.AcceptTcpClient();
netStream = client.GetStream();
Invoke(new PrintMsg(printMsg), new object[] { "Found client" });
//send command to client
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes("Hello client");
netStream.Write(buffer, 0, buffer.Length);
netStream.Flush();
}
catch (Exception ex)
{
Invoke(new PrintMsg(printMsg), new object[] { ex.ToString() });
}
}
而且客戶端代碼:
while True:
msg = s.recv(1024)
print 'SERVER:', msg
所以我想「放」到緩衝區數組整數,然後將其發送到python腳本。可能嗎?我究竟做錯了什麼?
在TCP,你不能只是'發送'和'recv'消息,而不需要某種分隔符,頭部等。一個'send'可以在另一端作爲四個'recv'到達,或者與另外三個'send'捆綁在一起。 – abarnert 2013-02-27 21:07:27