简体   繁体   中英

How to connect to tcp port 23 for telnet using C# application?

 Here is my code:
    using System; 
    using System.Net; 
    using System.Net.Sockets; 
    using System.Text; 
   class TcpClientSample 
   {
    public static void Main() 
   { 
    byte[] data = new byte[1024]; 
    string input, stringData; 
    TcpClient server; 
    /*try
       {
     TcpListener tcpListener = new TcpListener("192.168.0.102", 23);
        tcpListener.Start();
        }
        catch(SocketException)
        {
        Console.WriteLine("Server is not listening");
        return;
        }*/
    try 
    { 
    //IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
    //server = new TcpClient(iep); 
    //server.Connect("192.168.0.104", 23);
    //TcpClient server= new TcpClient(); 
    //server.Connect("192.168.0.104", 23);
    server = new TcpClient("192.168.1.1", 23); 
    } catch (SocketException) 
    { 
    Console.WriteLine("Unable to connect to server"); 
    return; 
    } 
    //NetworkStream ns = new NetworkStream();
    NetworkStream ns = server.GetStream(); 
    int recv = ns.Read(data, 0, data.Length); 
    stringData = Encoding.ASCII.GetString(data, 0, recv); 
    Console.WriteLine(stringData); 
    while(true) 
    { 
    input = Console.ReadLine(); 
    if (input == "exit") 
    break; 
    ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length); 
    ns.Flush(); 
    data = new byte[1024]; 
    recv = ns.Read(data, 0, data.Length); 
    stringData = Encoding.ASCII.GetString(data, 0, recv); 
    Console.WriteLine(stringData); 
    } 
    Console.WriteLine("Disconnecting from server..."); 
    ns.Close(); 
    server.Close(); 
    }

}

I want to telnet my network device using c# application it is working when i am making my own server app at some other port like 9050 but is is not connecting at port 23 which is specified port for telnet when i am trying to telnet it. Any help would be highly appreciable Plz!!!

this is the error I get in debug mode 在此处输入图片说明

This must be some AV (Antivirus) Tool / Firewall issue blocking your Port 23 . Try deactivating them and test your code again. If this doesn't help. Follow this.

  1. Use network monitor on W 7 and UNIX side to pinpoint is there is any communication.
  2. CurrentControlSet1 is not effective and the chnage of SackOpts would not be seen I guess
  3. If nothing helps run putty to test connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM