如何使用 TCPListener(或其他方法)绑定到网络上的设备
我有一个网络地址为 192.168.xxx.xxx 端口 xxxx 的设备。这是我的网络上的有效地址。我尝试使用 TCPListener 建立服务器连接,但收到错误 “错误...... System.Net.Sockets.SocketException:请求的地址在其上下文中无效 在 System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress 套接字地址) 在 System.Net.Sockets.Socket.Bind(EndPoint localEP) 在 System.Net.Sockets.TcpListener.Start(Int32 积压) 在 System.Net.Sockets.TcpListener.Start() 在 C:.....\Server.cs:line 49" 中的 WinMarkTest.Server.Main() 处
当我使用 myListener.Start() 方法时,
。 “本地地址”是否意味着使用 TCPListener 的服务器的地址?
我还能如何建立此连接?该设备位于我的内部网络(防火墙的我这边)内。
I have a device with a network address of 192.168.xxx.xxx port xxxx. This is a valid address on my network. I have tried to use TCPListener to make the connection for the server but receive the error
"Error..... System.Net.Sockets.SocketException: The requested address is not valid in its context
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress
socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at System.Net.Sockets.TcpListener.Start()
at WinMarkTest.Server.Main() in C:.....\Server.cs:line 49"
when I use the myListener.Start() method.
Does the "local address" mean the address of the server to use TCPListener?
How else can I make this connection. The device is within my internal network (my side of the firewall).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TcpListener 将在您的服务器上运行,等待与特定端口的连接。 TcpClient 将用于建立到 192.168.xxx.xxx:xxxx 地址的连接。因此,当您执行 Listener.Start 时,您正在侦听在侦听器运行的地址和端口上与侦听器建立的连接。本地地址确实意味着您正在侦听要建立的连接的地址。
如果您想连接到远程 ip:port 那么您应该尝试 TcpClient。一个简单的测试是看看您是否可以连接到 smtp 服务器或类似的服务器。
编辑:-- 包括一个非常粗略的示例,用于连接 pop.google.com 并发送/接收数据。
TcpListener would be running on your server waiting for connections to a particular port. A TcpClient would be used to make a connection to the 192.168.xxx.xxx:xxxx address. So when you do a Listener.Start you are listening for connections to be made to your listener on the address and port where the listener is running. The local address does mean the address address that you are listening for connections to be made on.
If you want to make a connection to a remote ip:port then you should try the TcpClient. An easy test would be to see if you could connect to an smtp server or something like that.
EDIT: -- Included a very crude example to connect and send/receive data to pop.google.com.