Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#TcpClient无法从成功连接的telnet服务器/设备读取有效响应_C#_Response_Tcpclient - Fatal编程技术网

c#TcpClient无法从成功连接的telnet服务器/设备读取有效响应

c#TcpClient无法从成功连接的telnet服务器/设备读取有效响应,c#,response,tcpclient,C#,Response,Tcpclient,目前我正在从事一个网络项目,其中包括连接tcp服务器和一些设备。很长一段时间以来,我成功地使用下面的代码来发送tcp命令和接收某些设备型号的响应,但当我在Windows Telnet Server 2003和Fortigate设备上尝试时,尽管我可以成功地建立tcp连接,但我能得到的响应是一些字符集,如???% 我接收响应的功能是: private string receive() { StringBuilder sbReadBuffer = new StringB

目前我正在从事一个网络项目,其中包括连接tcp服务器和一些设备。很长一段时间以来,我成功地使用下面的代码来发送tcp命令和接收某些设备型号的响应,但当我在Windows Telnet Server 2003和Fortigate设备上尝试时,尽管我可以成功地建立tcp连接,但我能得到的响应是一些字符集,如???%

我接收响应的功能是:

   private string receive()
    {
        StringBuilder sbReadBuffer = new StringBuilder();
        bool isFinished = false;
        NetworkStream ns = tcpClient.GetStream();
        DateTime lastConTime = DateTime.Now;

        while (!isFinished)
        {
            if (!ns.DataAvailable)
            {
                Thread.Sleep(200);

                if ((DateTime.Now - lastConTime).TotalSeconds > Prms.tcpSendReceiveTimeoutInSeconds)
                    break;
                else
                    continue;
            }

            Int32 receiveCount = ns.Read(receiveBuffer, 0, receiveBuffer.Length);
            String received = new ASCIIEncoding().GetString(receiveBuffer, 0, receiveCount);

            sbReadBuffer.Append(received);

            foreach (String terminaterToken in terminaterTokens)
                if (sbReadBuffer.ToString().EndsWith(terminaterToken))
                    isFinished = true;

            lastConTime = DateTime.Now;
        }

        return sbReadBuffer.ToString();
    }
}
用于建立tcp连接和调用接收功能:

        TcpClient tcpClient = new TcpClient()
    {
        SendTimeout = 10000,
        ReceiveTimeout = 10000
    };

    tcpClient.Connect(wanIp, 23);
    String initialMessage = receive();
我测试了上述代码正常工作的设备收到的消息,我意识到对于每个不同的设备型号,初始响应总是类似于“?%”,但随后是逻辑响应,如“欢迎”与“欢迎”

在我的测试中,我发现程序成功地连接到服务器/设备,但无法收到有效的响应,我不知道为什么。对此有何看法?

导致“?%”符号的问题是由于telnet选项IAC。返回响应值时,IAC的十进制代码大于255。有关IAC的更多信息,请访问以下地址

support.microsoft.com/kb/231866

IAC值的问题在于,当您尝试使用标准代码用C#tcpClient解析响应时

            byte[] sendBuffer = new ASCIIEncoding().GetBytes(str + strNewLine);
            tcpClient.GetStream().Write(sendBuffer, 0, sendBuffer.Length);
当收到IAC值时,代码生成上述“?%”值

为了防止这种情况发生并处理IAC值,我做了一些研究,在地址上发现了一个优秀的telnet项目,它可以完美地处理IAC值

为了更好地理解IAC值的处理,还需要检查MinimalisticTelnet库的源代码。使用MinimalisticTelnet,我能够毫无问题地获得响应,但它无法为我工作的设备发送消息,因此我更喜欢telnet套接字项目。但我强烈建议学习MinimalisticTelnet源代码,以便更好地理解