Telnet 不发送数据
Telnet not sending data
我正在尝试将数据传递到 telnet 服务器。
我得到了登录凭据的初始提示,但它不会使用用户名。
我需要传递用户名,等待密码提示并输入。
然后就可以发送和接收数据,一直保持套接字打开。
我的代码看起来像这样(它很粗糙,因为我首先在控制台中尝试)
try
{
client = new TcpClient("10.0.0.94",23);
Console.WriteLine("Connected to server.");
}
catch (SocketException)
{
Console.WriteLine("Failed to connect to server");
return;
}
//Assign networkstream
NetworkStream networkStream = client.GetStream();
byte[] data = new byte[1024];
int size = networkStream.Read(data, 0, data.Length);
recieved = Encoding.ASCII.GetString(data, 0, size);
Console.WriteLine(recieved);
if (recieved.Contains("login"))
{
string loginrx;
string cmd = string.Format("{0}\r",user) ;
byte[] writeBuffer = Encoding.ASCII.GetBytes(cmd);
networkStream.Write(writeBuffer, 0, writeBuffer.Length);
byte[] logindata = new byte[1024];
int loginsize = networkStream.Read(logindata, 0, logindata.Length);
loginrx = Encoding.ASCII.GetString(logindata, 0, loginsize);
Console.WriteLine(loginrx);
Console.ReadLine();
}
我收到登录提示,但一切都到此为止了。
任何帮助都会很棒。
一次发送不等于一次接收。你需要一些方法来告诉你你在 "end of a message" (可能是换行符)。请参阅 Message Framing 了解一些学习资料。
您将需要查阅 telnet 协议,但您可能需要继续阅读,直到您换行阅读,然后再检查您收到的文本。您可能希望使用带有 sr.ReadLine()
的 var sr = new StreamReader(networkStream, Encoding.ASCII)
来读取字符串,而不是手动调用 networkStream.Read
并使用 Encoding.ASCII.GetString
进行解码。
这很有效
while(Connected == false)
{
byte[] data = new byte[1024];
int size = networkStream.Read(data, 0, data.Length);
recieved = Encoding.ASCII.GetString(data, 0, size);
Console.WriteLine(recieved);
if (recieved.Contains("login"))
{
login = string.Format("{0}\r\n",user);
Console.WriteLine("user request found:{0}", login);
}
else if (recieved.Contains("password"))
{
login = string.Format("{0}\r\n",pass);
Console.WriteLine("password request found:{0}", login);
}
else if (recieved.Contains("GNET"))
{
Console.WriteLine(recieved);
Connected = true;
}
byte[] loginBuffer = Encoding.ASCII.GetBytes(login);
networkStream.Write(loginBuffer, 0, loginBuffer.Length);
networkStream.Flush();
}
我正在尝试将数据传递到 telnet 服务器。 我得到了登录凭据的初始提示,但它不会使用用户名。 我需要传递用户名,等待密码提示并输入。 然后就可以发送和接收数据,一直保持套接字打开。
我的代码看起来像这样(它很粗糙,因为我首先在控制台中尝试)
try
{
client = new TcpClient("10.0.0.94",23);
Console.WriteLine("Connected to server.");
}
catch (SocketException)
{
Console.WriteLine("Failed to connect to server");
return;
}
//Assign networkstream
NetworkStream networkStream = client.GetStream();
byte[] data = new byte[1024];
int size = networkStream.Read(data, 0, data.Length);
recieved = Encoding.ASCII.GetString(data, 0, size);
Console.WriteLine(recieved);
if (recieved.Contains("login"))
{
string loginrx;
string cmd = string.Format("{0}\r",user) ;
byte[] writeBuffer = Encoding.ASCII.GetBytes(cmd);
networkStream.Write(writeBuffer, 0, writeBuffer.Length);
byte[] logindata = new byte[1024];
int loginsize = networkStream.Read(logindata, 0, logindata.Length);
loginrx = Encoding.ASCII.GetString(logindata, 0, loginsize);
Console.WriteLine(loginrx);
Console.ReadLine();
}
我收到登录提示,但一切都到此为止了。
任何帮助都会很棒。
一次发送不等于一次接收。你需要一些方法来告诉你你在 "end of a message" (可能是换行符)。请参阅 Message Framing 了解一些学习资料。
您将需要查阅 telnet 协议,但您可能需要继续阅读,直到您换行阅读,然后再检查您收到的文本。您可能希望使用带有 sr.ReadLine()
的 var sr = new StreamReader(networkStream, Encoding.ASCII)
来读取字符串,而不是手动调用 networkStream.Read
并使用 Encoding.ASCII.GetString
进行解码。
这很有效
while(Connected == false)
{
byte[] data = new byte[1024];
int size = networkStream.Read(data, 0, data.Length);
recieved = Encoding.ASCII.GetString(data, 0, size);
Console.WriteLine(recieved);
if (recieved.Contains("login"))
{
login = string.Format("{0}\r\n",user);
Console.WriteLine("user request found:{0}", login);
}
else if (recieved.Contains("password"))
{
login = string.Format("{0}\r\n",pass);
Console.WriteLine("password request found:{0}", login);
}
else if (recieved.Contains("GNET"))
{
Console.WriteLine(recieved);
Connected = true;
}
byte[] loginBuffer = Encoding.ASCII.GetBytes(login);
networkStream.Write(loginBuffer, 0, loginBuffer.Length);
networkStream.Flush();
}