TCP响应缩短了

时间:2012-11-09 12:05:38

标签: c# windows-phone-7 networking tcp

我在WP7中使用TCP客户端。目前我只是使用MSDN的示例代码,所以它应该工作。但由于某种原因,这种特殊的反应正在缩短。

应该回复(后跟缓冲区中的大量空字节):

  

202-多行响应遵循\ r \ n时间戳= 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"; }

1 个答案:

答案 0 :(得分: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; }
    }