0

我在 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"; }
4

1 回答 1

1

要修复它,您必须检查响应是否是多行的。如果是,则循环直到它以“.\r\n”结束。否则你读一次就完成了。像这样:

public string GetFromTextCommand(string command)
    {
        try
        {
            if (!_isConnected)
                Connect();
            if (!_isConnected)
                return null;

            SendTextCommand(command);

            string response = GetFromTextCommand();

            if (response.StartsWith("202"))
            {
                while (!response.EndsWith(".\r\n"))
                {
                    string newResponse = GetFromTextCommand();

                    response += newResponse;
                }
            }

            return response;
        }
        catch (Exception ex) { GenerateException(ex.Message); return null; }
    }
    public string GetFromTextCommand()
    {
        try
        {
            if (!_isConnected)
                Connect();
            if (!_isConnected)
                return null;

            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 null; }
    }
于 2012-11-09T12:52:38.123 回答