c# Send telnet command WinForms

Permudious

New Member
Dec 28, 2014
19
0
Hiya!

I am developing a tool which is using Tenet. I would like to send a command.
I have test this directly in Windows so the parameters should work, but I have no Idea how I can send a command when I press on a button.

This is the code below;

Please keep in mind;

IP = 127.0.0.1 (localhost)
Port = 4000
Username = not needed
Password = not needed

C#:
using (var client = new Client("127.0.0.1", 4000, new System.Threading.CancellationToken()))
    {
        if (client.IsConnected)
        {
            var s = new System.Text.StringBuilder();
            s.Append("-red");
            client.Write(s.ToString());
        }
    }

Thanks in advance!
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
I'd recommend avoiding Telnet as it is an old unencrypted protocol. You'd probably be better using something like UNIX (or regular TCP) sockets for this, depending on what you're trying to achieve. Is the host always on the local machine, or sometimes remote?

You can use for that or . You'll need to create a listener on the server to read the incoming data (see example: ).

As for the button press thing, you can probably create an event handler ( ) to catch when the button is pressed and then call this code.
 

Permudious

New Member
Dec 28, 2014
19
0
I'd recommend avoiding Telnet as it is an old unencrypted protocol. You'd probably be better using something like UNIX (or regular TCP) sockets for this, depending on what you're trying to achieve. Is the host always on the local machine, or sometimes remote?

You can use for that or . You'll need to create a listener on the server to read the incoming data (see example: ).

As for the button press thing, you can probably create an event handler ( ) to catch when the button is pressed and then call this code.

Thanks! let me try TcpClient.Connect. It's always a local machine.
Post automatically merged:

Okay it's kinda working for now.. only I have an if, else if, else case which checks a value if the value is < 10 than the message is -green if the value = 10 than the message is -red.
The value can be changed when the timer is ticking. So I assume that I need to close the tcp connect first before I can open it again. I have tried that but that isn't working.

C#:
if (TotalIn <= 10)
{
    var client = new TcpClient();
    var hostname = "127.0.0.1";
    client.Connect(hostname, 4000);

    NetworkStream networkStream = client.GetStream();
    networkStream.ReadTimeout = 5;

    var writer = new StreamWriter(networkStream);

    var message = "-green";
    var reader = new StreamReader(networkStream, Encoding.UTF8);

    byte[] bytes = Encoding.UTF8.GetBytes(message);
    networkStream.Write(bytes, 0, bytes.Length);
    client.Close();
}
else if (TotalIn == 1)
{
    var client = new TcpClient();
    var hostname = "127.0.0.1";
    client.Connect(hostname, 4000);

    NetworkStream networkStream = client.GetStream();
    networkStream.ReadTimeout = 5;

    var writer = new StreamWriter(networkStream);

    var message = "-red";
    var reader = new StreamReader(networkStream, Encoding.UTF8);

    byte[] bytes = Encoding.UTF8.GetBytes(message);
    networkStream.Write(bytes, 0, bytes.Length);
    client.Close();
}
else
{
    // no message
}

Whatever I do, the message will always send -green but the value is now 10, so it should be -red.
 
Last edited:

Users who are viewing this thread

Top