【发布时间】:2015-12-16 06:30:07
【问题描述】:
我尝试在 C# 应用程序中使用 telnet 连接到我的 TeamSpeak 3 服务器。
顺便说一句,我使用telnet的经验不是很丰富^^',所以我在网站上显示了telnet代码 https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient(VS.80).aspx
以下代码应该:
- 连接到 teampeak 服务器
- 发送密码并读出欢迎信息
-
发送命令“help”并读出帮助信息
string command = "help"; // creates new TCP client TcpClient client = new TcpClient(adress, port); // get client stream NetworkStream stream = client.GetStream(); // send Password Byte[] data = System.Text.Encoding.ASCII.GetBytes(password); stream.Write(data, 0, data.Length); data = new Byte[256]; Thread.Sleep(200); Int32 bytes = stream.Read(data, 0, data.Length); String responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); Console.WriteLine(responseData); // send the given command Byte[] data2 = System.Text.Encoding.ASCII.GetBytes(command); stream.Write(data2, 0, data2.Length); data2 = new Byte[2560]; Thread.Sleep(200); Int32 bytes2 = stream.Read(data2, 0, data2.Length); String responseData2 = System.Text.Encoding.ASCII.GetString(data2, 0, bytes2); Console.WriteLine(responseData2); // end stream and client stream.Close(); client.Close();
第一个查询正常工作,并将欢迎消息写入控制台。但在第二个查询中的 Int32 bytes2 = stream.Read(data2, 0, data2.Length); 处,应用程序停止,没有返回任何异常。
谁能解释为什么我无法读出帮助消息?
【问题讨论】:
-
如果您不反对使用库,我有一个位于 nuget.org/packages/Telnet 的 NuGet 包(代码位于 github.com/9swampy/Telnet),它可以为您完成所有 telnet 通信......
-
感谢您的提议,但是当我可以自己实现所有这些时,我总是很高兴。这样我就知道我在做什么。 ^^
-
是的,我也有点这样。如果您仍然卡住,请查看 Github 上的代码。祝你好运。