-1

I want to communicate with a Telnet server using C# in .NET Framework 4.6.2. I just simply want to send a command and get its response. I have already tried using Socket programming and Telnet library. In both cases, I am receiving the command as well as the response in the output. That is if I send the command Status and its response from the telnet server is Online in output I get Status\r\rOnline\r\n.

Also, one more problem I am facing with the Telnet library is that it provides async methods and I am restricted to programming in synchronous methods by the underlying SDK I am using.

So, I would like to know if there is a straightforward way to communicate with the telnet server using C# in .NET Framework 4.6.2.

Hem Bhagat
  • 323
  • 3
  • 12
  • So, you want to disable ECHO, right? See https://stackoverflow.com/questions/4689893/is-it-possible-to-disable-echo-when-using-socket-for-telnet – rene Aug 12 '22 at 07:58
  • One question per post please. If you are asking for a recommended library, that is off-topic for [so], please use [softwarerecs.se] – Charlieface Aug 12 '22 at 08:17
  • If you want to handle standard Telnet, then you need to read [the Telnet specifications](https://en.wikipedia.org/wiki/Telnet#Related_RFCs). To handle all the proper telnet options and negotiations is not trivial (but fun, I've done it a few times). You might want to find an existing library that help you with it. – Some programmer dude Aug 12 '22 at 08:31
  • @Charlieface The question is about how to communicate with a Telnet server using C#. I have mentioned the library as one of the solutions I have tried. I have mentioned it so that I don't receive already tried solutions in the answers. The answer to the question can be a library suggestion or a native c# code. Regardless of that, if you feel that the question can be reworded to make it more comprehensible, you can edit it. – Hem Bhagat Aug 12 '22 at 09:10
  • Showing the code you tried might help with echo problem. A generalized "what is the best library?" question is not generally a good fit for [so]. If you want to keep this quesion here rather than [softwarerecs.se] I suggest you pick the first question, show your code, and reword to show you are asking for help with your existing code. – Charlieface Aug 12 '22 at 09:54

1 Answers1

0

https://www.nuget.org/packages/telnet

using FluentAssertions;
using System;
using System.Text.RegularExpressions;
using PrimS.Telnet

private const int TimeoutMs = 5000;

using (Client client = new Client(
  server.IPAddress.ToString(), 
  server.Port, 
  new System.Threading.CancellationToken()))
{
  client.IsConnected.Should().Be(true);
  Client.IsWriteConsole = true;
  client.TryLogin(
    "username", 
    "password", 
    TimeoutMs).Should().Be(true);
  client.WriteLine("status");
  string s = client.TerminatedRead(
    ">", 
    TimeSpan.FromMilliseconds(TimeoutMs));
  s.Should().Contain("Online");
}
9swampy
  • 1,441
  • 14
  • 24