Sohail
Sohail

Reputation: 780

How to do telnet in C# console application

In C# console application need to perform telnet utility and know whether the required port is open

   var ping = new Ping();
        var rply = ping.Send("192.168.1.117");
        if (rply.Status == IPStatus.Success)
        {
            Console.WriteLine("up");
            Console.WriteLine("Press any key to continue");
            Console.ReadKey(true);

          }
        else
        {
            Console.WriteLine("down");
            Console.WriteLine("Press any key to continue");
            Console.ReadKey(true);

I am using above code to ping but for telnet and port how should i do it, so that in console application it should perform telnet utility and let the user know the required port is open

Upvotes: 1

Views: 8431

Answers (1)

CrazyCasta
CrazyCasta

Reputation: 28362

Generally speaking, server admins don't like you connecting and then dropping (the only real way to see if a port is open). However, if you want to do it you can do:

TcpClient tc = null;
try
{
    tc = new TcpClient("192.168.1.117", 23);
    // If we get here, port is open
} 
catch(SocketException se) 
{
    // If we get here, port is not open, or host is not reachable
}
finally
{
   if (tc != null)
   {
      tc.Close(); 
   }
}

Upvotes: 6

Related Questions