0

WP7でTCPクライアントを使用しています。現時点では、MSDNのサンプルコードを使用しているだけなので、機能するはずです。しかし、何らかの理由で、この特定の応答は短縮されています。

応答する必要があります(バッファからの多くのnullバイトが続きます):

202-複数行の応答が続きます\r\ ntimestamp = 0x00000000checksum = 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 に答える