zibidyum
zibidyum

Reputation: 174

c# TcpClient can not read valid response from successfully connected telnet server / device

Currently I am working on a network project which includes connecting to tcp servers and some devices. For a long time I successfully used below code for sending tcp command and receiving responses for some device models, but when I try it for Windows Telnet Server 2003 and a Fortigate device, even though I can build tcp connection successfully all I can get as response is some character set like ??%??????'????.

My function for receiving response is:

   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();
    }
}

and for building tcp connection and calling receive function:

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

    tcpClient.Connect(wanIp, 23);
    String initialMessage = receive();

I tested received messages for the devices that above code works without a problem and I realized for each different device model the initial response is always something like "??%??????'????" but it followed by logical responses like "welcome" vs.

During my tests I discovered the program connects to server/device successfully but can not receive valid responses, which I do not know why. Any idea about this?

Upvotes: 0

Views: 2401

Answers (1)

zibidyum
zibidyum

Reputation: 174

The problems causing "??%??" symbols is because of the telnet option IAC. Returning response values as IAC has bigger decimal codes than 255. Further information about IAC can be reached from the address

support.microsoft.com/kb/231866

The problem with IAC values is that when you try to parse the responses with C# tcpClient using standart code

            byte[] sendBuffer = new ASCIIEncoding().GetBytes(str + strNewLine);
            tcpClient.GetStream().Write(sendBuffer, 0, sendBuffer.Length);

the code produces the above "??%" values when an IAC values received.

To prevent this and handling IAC values I did some research and found an excellent telnet project on the address http://www.codeproject.com/KB/IP/TelnetSocket.aspx which perfectly handles the IAC values.

For better understanding handling IAC values also checking MinimalisticTelnet library's source code will be useful. I was able to get responses without a problem using MinimalisticTelnet but it failed on sending messages for the devices I work on so I preferred telnet socket project. But I strongly suggest to study MinimalisticTelnet source code for better understanding.

Upvotes: 1

Related Questions