Indy tcp 服务器 - 确定服务器的 IP 和端口
如何使用delphi确定IdTcpServer的“互联网”IP和端口?不是本地(127.0.0.1 或 192.168.0.1),而是公共互联网。
How to determine the IdTcpServer's 'internet' ip and port using delphi? Not the local (127.0.0.1 or 192.168.0.1) but the public internet ones.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
端口和绑定
端口号很容易确定,如果您愿意检查 TIdTcpServer,您会注意到它有一个
DefaultPort
属性和Bindings
因此,如果您询问它“接受连接”的端口,它应该是 DefaultPort,或者Bindings
中配置的任何端口。一旦会话处于活动状态,该会话将在其自己的动态分配的端口号上处于活动状态。如果在我的应用程序中,我将 IdTcpServer1.DefaultPort 设置为 90,然后在运行时转储绑定的内容,我会得到以下列表:
上面显示全零的列表是 IP 地址通配符表示法,告诉我我正在监听在端口 90 处的所有可用本地 IPV4 和 IPV6 地址上。我用来转储上述列表的代码是:
公共 IP 地址
所以让我们只回到 IP 地址,并忽略端口,对于这个问题的其余部分:
您的计算机可能有一个以太网卡,或一个无线网络适配器,或两者都有,并且这两者中的任何一个都可以有自己的私有地址。其次,如果您是网络管理员并且您已经给了计算机中的任何网络适配器一个公共 IP,那么您计算机中的任何网络适配器也都可以有一个公共 IP。如果您没有配置您正在使用的网络,那么您在计算机上使用可公开访问的 IP 的可能性非常小。
如果您首先查看此处,它会告诉您如何操作,那么您的问题会更好确定您的计算机可以通过哪些 IP 进行访问(枚举所有本地 IP)。然后,您可以询问有关过滤该列表的问题,并通过丢弃私有的不可路由地址来找到正确的列表。但我敢打赌它不适用于您的情况...让我们找出原因...
如果您是普通家庭或企业台式电脑用户,在您的电脑上使用私有 IP 地址,并且您的互联网连接通过网络地址转换 (NAT) 发生,那么答案是您的 PC 甚至没有公共地址。
可以配置基于 NAT 的路由器来转发端口,但我不知道有什么方法可以让转发的接收者知道转发到您的计算机的端口的存在。如果您需要帮助在家中配置基于 NAT 的 DSL 或电缆调制解调器以将端口转发到 Delphi 应用程序,那么最好到其他地方询问。
Ports and Bindings
The port number is easily determined, if you care to inspect TIdTcpServer, you'll notice it has a
DefaultPort
property, andBindings
and so if you are asking what port it is "accepting connections on", it should be the DefaultPort, or whatever is configured insideBindings
. Once you have a session active, that session will be active on its own port number which is dynamically assigned.If in my app, I set the IdTcpServer1.DefaultPort to 90, and then dump the contents of the bindings at runtime I get this list:
The above list showing all zeroes is IP-address-wildcard notation, telling me that I'm listening on all available local IPV4 and IPV6 addresses, at port 90. The code I used to dump the above list is:
Public Ip Addresses
So lets go back to IP addresses only, and ignore ports, for the rest of this question:
Your computer probably has an ethernet card, or a wireless network adaptor, or both, and either of both of those, could each have their own private address. Secondly, any network adapter in your computer could also have a public IP if you are the network administrator and you've given it one. If you did not configure the network you are using, chances are very slim that you're using a publically accessible IP at all on your machine.
Your question would be better if you first looked here which tells you how to determine what IPs your computer is accessible as (enumerate all local IPs). Then you could ask a question about filtering that list, and finding the correct one by discarding private non-routable addresses. Except that I bet it won't work in your case... Let's figure out why...
If you are an average home or business desktop PC user, who is using a private-IP address on your PC, and your internet connection happens via Network Address Translation (NAT), then the answer is that your PC doesn't even have a public address.
It is possible to configure a NAT based router to forward ports, but I am unaware of any way for the recipient of that forwarding to be aware of the existence of a port forward to your machine. If you need help configuring your NAT based DSL or cable modem at home to forward a port to your Delphi application, that would be a better question to ask somewhere else.