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();
}
}
我想使用 c# 应用程序远程登录我的网络设备,当我在 9050 等其他端口创建自己的服务器应用程序时,它正在工作,但没有在端口 23 连接,这是我尝试远程登录时指定的 telnet 端口。任何帮助都将是非常可观的PLZ!