C# – Send and Receive commands via Telnet in C#

ccommunicationip addresstelnetwpf

I'm new with Telnet, and I need to write a small program in WPF (C#) which send and receive commands to a device via telnet.

Its starts by:

  1. sending command "telnet 192.168.0.50" (in order to connect to the device)
  2. Then sending "User" and "Password"
  3. Then starting to send and receive data to the device, and all that communication need to appears on a WPF log screen.

I have search on Google information on how to do it, but I didn't find something which was helpful for me.

I'm asking here because I'm helpless and I need some directions in order to starts from somewhere… I'll really appreciate your help.

Thank you.

Best Answer

I've a GitHub project https://github.com/9swampy/Telnet/, also published as a NuGet dll https://www.nuget.org/packages/Telnet that would allow you to:

using (Client client = new Client(server.IPAddress.ToString(), server.Port, new System.Threading.CancellationToken()))
{
  await client.TryLoginAsync("username", "password", TimeoutMs);
  client.WriteLine("whatever command you want to send");
  string response = await client.TerminatedReadAsync(">", TimeSpan.FromMilliseconds(TimeoutMs));
}

That should allow you to achieve what you want in your application, and you can check out the GitHub if you want to see the code making it all work.

Related Topic