简体   繁体   English

如何找到计算机的IP地址?

[英]How do I find a computer's IP address?

I am writing a program in C++ which uses network sockets. 我正在用C ++编写一个使用网络套接字的程序。 I need to find out what the computer's IP address is, so I can display it to the user. 我需要找出计算机的IP地址,以便将其显示给用户。 The program must run on Windows and Linux. 该程序必须在Windows和Linux上运行。

I have heard somewhere that a computer can have multiple IP addresses. 我听说某处计算机可以有多个IP地址。 I want the one that other programs on different computers can use to connect to the computer. 我希望其他计算机上的其他程序可以使用该程序来连接到计算机。

Here is the relevant code I already have (the variables are declared in other places): 这是我已经拥有的相关代码(变量在其他地方声明):

master = new fd_set;
FD_ZERO(master);
struct sockaddr_in my_addr;

listener = socket(PF_INET, SOCK_STREAM, 0);

my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);

bind(listener, (struct sockaddr *)&my_addr, sizeof my_addr);

listen(listener, 10);

FD_SET(listener, master);

fdmax = listener;

I have heard somewhere that a computer can have multiple IP addresses. 我听说某处计算机可以有多个IP地址。 I want the one that other programs on different computers can use to connect to the computer. 我希望其他计算机上的其他程序可以使用该程序来连接到计算机。

Well... that could be any of them. 好吧...可能是其中任何一个。 If a computer has multiple IP addresses it can be accessed on any one of them. 如果计算机具有多个IP地址,则可以在其中任何一个上访问它。 Of course one of them could be subject to different firewall rules or they could be on two completely different segments but there's no way to detect any and all of these circumstances. 当然,其中之一可能受制于不同的防火墙规则,或者它们可能位于两个完全不同的网段,但是无法检测到所有这些情况。

I posted a similar question, but on OS X recently. 我发布了一个类似的问题,但是最近在OS X上发布了。 The answer that I received was to use either 0.0.0.0 or INADDR_ANY. 我收到的答案是使用0.0.0.0或INADDR_ANY。 This will cause your socket to listen on all available addresses, so you don't need to figure out which one is the "right" one. 这将使您的套接字监听所有可用的地址,因此您无需弄清楚哪个是“正确的”地址。

On Windows, you want to use GetAdaptersAddresses - this lists all of the adapters in your machine and the IP addresses bound to them. 在Windows上,您要使用GetAdaptersAddresses-这列出了计算机中的所有适配器以及绑定到它们的IP地址。 It supports IPv6 addresses too. 它也支持IPv6地址。 You can also use gethostbyname , but that doesn't support IPv6. 您还可以使用gethostbyname ,但这不支持IPv6。

On Linux, we read /proc/net/dev and /proc/net/if_inet6 and parse the results of that. 在Linux上,我们读取/proc/net/dev/proc/net/if_inet6并解析其结果。

我相信您可以将getaddrinfo()与侦听器套接字一起使用,以获取绑定到的套接字的IP地址。

It depends if you're trying to get your LAN IP address (ie the address of your computer within the set of your computers) or the IP address your service provider gives to you every time you connect to the internet. 这取决于您是要获取LAN IP地址(即计算机集中的计算机地址)还是服务提供商每次连接到Internet时提供给您的IP地址。 The latter can be identified with a query (I guess that you would find a proper C++ library that does that with little Googling) to some IP detecting web services. 后者可以通过查询来识别某些IP检测Web服务(我想您会发现一个用很少的Googling就能做到的正确C ++库)。

If you want a quick and dirty solution you could try to wget http://www.whatismyip.org and read back the contents. 如果您想要一个快速而肮脏的解决方案,则可以尝试获取http://www.whatismyip.org并读回其中的内容。

您可以在C ++项目中使用轻量级的客户机/服务器套接字类作为参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM