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 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.
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.