从telnet服务器c#套接字读取响应
我有下面的代码,它成功地允许我打开到telnet服务器的套接字,并协商适当的握手,如下所示从telnet服务器c#套接字读取响应,c#,sockets,telnet,C#,Sockets,Telnet,我有下面的代码,它成功地允许我打开到telnet服务器的套接字,并协商适当的握手,如下所示 Established Connection to 192.168.10.33 FF FD 18 FF FD 20 FF FD 23 FF FD 27 FF FD 24 ÿy.ÿy.ÿy#ÿy'ÿy$ FF FB 03 FF FD 01 FF FD 22 FF FD 1F FF FB 05 FF ÿû.ÿy.ÿy"ÿy.ÿû.ÿ FD 21
Established Connection to 192.168.10.33
FF FD 18 FF FD 20 FF FD 23 FF FD 27 FF FD 24 ÿy.ÿy.ÿy#ÿy'ÿy$
FF FB 03 FF FD 01 FF FD 22 FF FD 1F FF FB 05 FF ÿû.ÿy.ÿy"ÿy.ÿû.ÿ
FD 21 y!
FF FB 01 FF FD 06 FF FD 00 ÿû.ÿy.ÿy.
FF FB 03 FF FB 01 ÿû.ÿû.
我陷入困境,似乎缺少一些基本的东西,那就是如何在上述握手完成后从telnet读回连续的数据流
这是一个概念。我发送一个存储在xml文件中的telnet命令,并希望能够从telnet服务器读回响应,作为一个变量,我可以使用它显示回控制台,并发送到应用程序中的其他方法。
请参见下图,了解使用腻子作为客户的说明:
我也无法收到0x0D 0x0A欢迎来到Tesira文本协议服务器0x0D 0x0A
欢迎消息,但我认为这只是更广泛问题的症状
我可以通过以下方式发送命令:
Console.WriteLine("Item: "+item.Attributes["commandText"].Value);
byte[] commandBytes = Encoding.ASCII.GetBytes(item.Attributes["commandText"].Value + " \r\n");
s.Send(commandBytes);
然而,我对如何读回数据有点迷茫。
下面的代码是我为处理套接字连接和握手方面而编写的类
我真的不知道下一步该往哪里走,我花了几个小时四处寻找一个好的教程,我可以理解,但没有运气找到任何真正对我有帮助的东西
class telnetHandshake
{
private static byte[] writeBuffer;
private static byte[] readBuffer;
private static int bc;
public void telnetInit()
{
IPAddress address = IPAddress.Parse("192.168.10.33");
int port = 23;
IPEndPoint endpoint = new IPEndPoint(address, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
s.Connect(endpoint);
Console.WriteLine("Established Connection to {0}", address);
byte[] readBuffer = new byte[1024];
bc = s.Receive(readBuffer);
//Console.WriteLine(bc + " Bytes Found");
DumpBytes(readBuffer, bc);
writeBuffer = new byte[] { 0xFF, 0xFC, 0x18, 0xFF, 0xFC, 0x20, 0xFF, 0xFC, 0x23, 0xFF, 0xFC, 0x27, 0xFF, 0xFC, 0x24 };
s.Send(writeBuffer);
readBuffer = new byte[1024];
bc = s.Receive(readBuffer);
//Console.WriteLine(bc + " Bytes Found");
DumpBytes(readBuffer, bc);
writeBuffer = new byte[] { 0xFF, 0xFE, 0x03, 0xFF, 0xFC, 0x01, 0xFF, 0xFC, 0x22, 0xFF, 0xFC, 0x1F, 0xFF, 0xFE, 0x05, 0xFF, 0XFC, 0x21 };
s.Send(writeBuffer);
readBuffer = new byte[1024];
bc = s.Receive(readBuffer);
//Console.WriteLine(bc + " Bytes Found");
DumpBytes(readBuffer, bc);
writeBuffer = new byte[] { 0xFF, 0xFE, 0x01, 0xFF, 0xFC, 0x06, 0xFF, 0xFC, 0x00 };
s.Send(writeBuffer);
readBuffer = new byte[1024];
bc = s.Receive(readBuffer);
//Console.WriteLine(bc + " Bytes Found");
DumpBytes(readBuffer, bc);
writeBuffer = new byte[] { 0xFF, 0xFE, 0x03, 0xFF, 0xFE, 0x01 };
s.Send(writeBuffer);
readBuffer = new byte[1024];
bc = s.Receive(readBuffer);
//Console.WriteLine(bc + " Bytes Found");
DumpBytes(readBuffer, bc);
}
catch
{
Console.WriteLine("Connection to {0} Failed!", address);
}
}
我不想使用minimal telnet或任何其他库,我想学习如何完成我已经开始的工作,因为我觉得我已经非常接近完成这项工作,它对我的目的非常有用。我能够使用以下代码解决我的问题:
if (s.Receive(readBuffer) > 0)
{
Console.WriteLine(hex2string(readBuffer, s.Receive(readBuffer)));
}
握手完成后,我只需检查任何新数据,如果有可用数据,我将其读入一个函数,该函数将其从十六进制转换为ASCII
这种方法对我很有效,因为代码段将在以下情况下调用:
byte[] commandBytes = Encoding.ASCII.GetBytes(item.Attributes["commandText"].Value + " \r\n");
s.Send(commandBytes);
因此,每次提交新命令时,都会有一个休止符等待读取。您可能需要使用ReadLine()。然后输入TELNET并按return。在继续之前,您的应用程序将等待返回。@jdweng我设置了一个readBuffer变量,检查是否有可读取的字节,将它们分配给readBuffer var,获取readBuffer长度,最后将十六进制字节数组转换为ASCii。不太清楚你的回答是什么意思。不过谢谢:)