Sarvesh Thakur
Sarvesh Thakur

Reputation: 79

How to open a Telnet session and send commands and receive responses programmatically?

I have an optoma projector and I want to communicate to it using Telnet session from inside the C# program. Using various wrappers and Nuget packages, I am able to start the telnet session but I am not abe to communicate commands or receive response in my C# program.

In a normal windows cmd, if I write Telnet 192.168.222.127, telnet session begins. The projector responds to commands in ASCII format(https://www.optoma.de/uploads/RS232/DS309-RS232-en.pdf) and it returns 'F' or 'P' based on if command Failed or passed. To change brightness to 10, I would do something like this:

  1. open a Telnet Connection : telnet 192.168.222.127
  2. Send a command to change brightness to 10 : ~0021 10
  3. Session will respond with a 'P' as the command passed in changing brightness. Telnet Terminal Commands

I want to do the same with C# program but I am stuck. Most of the answers point to outdated packages and link. I am new to Telnet and communication protocols and need help regarding this. Thank you.

Upvotes: 3

Views: 8466

Answers (1)

Sarvesh Thakur
Sarvesh Thakur

Reputation: 79

If you simply want to connect to the device, send in the command and read the response, simple C# TcpClient is enough. It provides client connections for TCP network services which was just right for my case.

For establishing connection :

using system.net.sockets;

    public void EstablishConnection(string ip_address, int port_number=23){
        try
        {
            client = new TcpClient(ip_address, port_number);
            if (DEBUG) Console.WriteLine("[Communication] : [EstablishConnection] : Success connecting to : {0}, port: {1}", ip_address, port_number);
            stream = client.GetStream();
        }
        catch
        {
            Console.WriteLine("[Communication] : [EstablishConnection] : Failed while connecting to : {0}, port: {1}", ip_address, port_number);
            System.Environment.Exit(1);
        }
    }

For sending and receiving response :

    public string SendMessage(string command)
    {
        // Send command
        Byte[] data = System.Text.Encoding.ASCII.GetBytes(command);
        stream.Write(data, 0, data.Length);
        if (DEBUG) Console.Write("Sent : {0}", command);

        WaitShort();

        // Receive response
        string response = ReadMessage();
        if (DEBUG) Console.WriteLine("Received : {0}", response);

        return response;
    }

    public string ReadMessage()
    {
        // Receive response
        Byte[] responseData = new byte[256];
        Int32 numberOfBytesRead = stream.Read(responseData, 0, responseData.Length);
        string response = System.Text.Encoding.ASCII.GetString(responseData, 0, numberOfBytesRead);
        response = ParseData(response);
        if (response == "SEND_COMMAND_AGAIN")
        {
            if (DEBUG) Console.WriteLine("[ReadMessage] : Error Retreiving data. Send command again.");
        }
        return response;
    }

You may parse the response from the server based on your requirements.

Upvotes: 4

Related Questions