wpf 在 C# 中通过 Telnet 发送和接收命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25116077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send and Receive commands via Telnet in C#
提问by Orionlk
I'm new with Telnet, and I need to write a small program in WPF (C#) which send and receive commands to a device via telnet.
我是 Telnet 的新手,我需要用 WPF (C#) 编写一个小程序,它通过 telnet 向设备发送和接收命令。
Its starts by:
它的开头是:
- sending command "telnet 192.168.0.50" (in order to connect to the device)
- Then sending "User" and "Password"
- Then starting to send and receive data to the device, and all that communication need to appears on a WPF log screen.
- 发送命令“telnet 192.168.0.50”(为了连接到设备)
- 然后发送“用户”和“密码”
- 然后开始向设备发送和接收数据,所有这些通信都需要出现在 WPF 日志屏幕上。
I have search on Google information on how to do it, but I didn't find something which was helpful for me.
我在 Google 上搜索了有关如何操作的信息,但没有找到对我有用的信息。
I'm asking here because I'm helpless and I need some directions in order to starts from somewhere... I'll really appreciate your help.
我在这里问是因为我很无助,我需要一些方向才能从某个地方开始......我会非常感谢你的帮助。
Thank you.
谢谢你。
回答by 9swampy
I've a GitHub project https://github.com/9swampy/Telnet/, also published as a NuGet dll https://www.nuget.org/packages/Telnetthat would allow you to:
我有一个 GitHub 项目https://github.com/9swampy/Telnet/,也作为 NuGet dll https://www.nuget.org/packages/Telnet发布,它允许您:
using (Client client = new Client(server.IPAddress.ToString(), server.Port, new System.Threading.CancellationToken()))
{
await client.TryLoginAsync("username", "password", TimeoutMs);
client.WriteLine("whatever command you want to send");
string response = await client.TerminatedReadAsync(">", TimeSpan.FromMilliseconds(TimeoutMs));
}
That should allow you to achieve what you want in your application, and you can check out the GitHub if you want to see the code making it all work.
这应该能让你在你的应用程序中实现你想要的,如果你想查看代码,你可以查看 GitHub。
回答by user3910810
Roughly (and in VB but should be simple to convert to c#). You'll just need to add the bits to write the username and password to the stream
粗略地(在 VB 中,但转换为 c# 应该很简单)。您只需要添加位以将用户名和密码写入流
Dim Data As String
Dim reader As StreamReader = Nothing
Dim NsStream As NetworkStream = Nothing
dim address as IPAddress = IPAddress.Parse("192.168.0.50")
Dim ipe As New IPEndPoint(address, port)
Dim telnetSocket As New Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
telnetSocket.Connect(ipe)
If (telnetSocket.Connected) Then
Try
NsStream = New NetworkStream(telnetSocket, True)
reader = New StreamReader(NsStream)
Do While (Not reader.EndOfStream)
' Read the line of data
Data = reader.ReadLine()
'Do whatever with data
Loop
Catch ex As Exception
Msgbox(ex.message)
Finally
Try
If reader IsNot Nothing Then
reader.Close()
End If
Catch ex As Exception
End Try
Try
If NsStream IsNot Nothing Then
NsStream.Close()
End If
Catch ex As Exception
End Try
End Try
end if

