C# TcpListener:监听每个地址,包括 GPRS IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/507254/
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
TcpListener: Listen on every address, including GPRS IP address
提问by TimothyP
We have a simple piece of legacy software with which we need to communicate using TCP/IP over port 15001. We need to listen on port 15001 for the legacy software to make a connection and then read whatever it sends us.
我们有一个简单的遗留软件,我们需要通过端口 15001 使用 TCP/IP 与之通信。我们需要在端口 15001 上侦听遗留软件以建立连接,然后读取它发送给我们的任何内容。
We have tested this solution accross the internet and it works just fine. If however we test the same solution across a GPRS TCP/IP network it does not.
我们已经在互联网上测试了这个解决方案,它工作得很好。但是,如果我们在 GPRS TCP/IP 网络上测试相同的解决方案,则不会。
All the basics have been checked, we can ping other devices in the GPRS network and port 15001 is not blocked by any firewall.
基本都检查过了,我们可以ping通GPRS网络中的其他设备,15001端口没有被任何防火墙阻止。
So maybe there is something wrong with my TcpListener?
所以也许我的 TcpListener 有问题?
It is initialized like this:
它是这样初始化的:
tcpServer = new TcpListener(IPAddress.Any, TCP_PORT);
I'm assuming it listens on every available IPv4 enabled interface on the system, because I used IPAddress.Any ?
我假设它侦听系统上每个可用的启用 IPv4 的接口,因为我使用了 IPAddress.Any ?
Does anybody have any idea what the difference might be between the two networks? (Even though there shouldn't be any difference) and if there is something I need to change to my TcpListener?
有人知道这两个网络之间可能有什么区别吗?(即使应该没有任何区别),如果有什么我需要更改为我的 TcpListener?
采纳答案by Jon B
You need to specify the IP address on which you want to listen, instead of IPAddress.Any. See here. When you use IPAddress.Any, it will automatically choose the network interface for you. To listen on a certain interface (in your case, GPRS) you have to use the correct IP in the constructor.
您需要指定要侦听的 IP 地址,而不是 IPAddress.Any。见这里。当您使用 IPAddress.Any 时,它会自动为您选择网络接口。要侦听某个接口(在您的情况下为 GPRS),您必须在构造函数中使用正确的 IP。
This posthas more information on getting the IP address for each nic.
这篇文章有更多关于获取每个网卡的 IP 地址的信息。
Also, if you're looking to listen on every IP address at once, you'll need a TcpListener for each nic.
此外,如果您希望一次侦听每个 IP 地址,则每个网卡都需要一个 TcpListener。