我在 WP7 中使用 TCP 客户端。目前我只是使用来自 MSDN 的示例代码,所以它应该可以工作。但由于某种原因,这种特殊的反应被缩短了。
它应该回复(后跟来自缓冲区的大量空字节):
202- 多行响应如下\r\ntimestamp=0x00000000 校验和=0x00000000\r\nname=\"FLASH:Flash\xshell.xex\"\r\n。\r\n
但相反,它正在返回(并且没有任何尾随空字节):
202- 多行响应如下\r\n
我从 TCP 服务器获取响应的代码是:
try
{
if (!_isConnected)
Connect();
if (!_isConnected)
return null;
SendTextCommand(command);
string response = "";
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
socketEventArg.UserToken = null;
socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
response = Encoding.ASCII.GetString(e.Buffer);
response = response.Trim('\0');
}
else
throw new Exception(e.SocketError.ToString());
_pausingThread.Set();
});
_pausingThread.Reset();
_socket.ReceiveAsync(socketEventArg);
_pausingThread.WaitOne(TIMEOUT_MILLISECONDS);
return response;
}
catch (Exception ex) { GenerateException(ex.Message); return "123"; }