在 C++ 中,获取本地计算机的 IP 地址和子网掩码的最简单方法是什么?
In C++, what's the easiest way to get the local computer's IP address and subnet mask?
我希望能够在我的本地网络中检测本地机器的 IP 地址.在我的特殊情况下,我有一个子网掩码为 255.255.255.0 的网络,我的计算机的 IP 地址为 192.168.0.5.我需要以编程方式让这些有两个值,以便向我的网络发送广播消息(对于我的特殊情况,格式为 192.168.0.255)
I want to be able to detect the local machine's IP address in my local network. In my particular case, I have a network with a subnet mask of 255.255.255.0 and my computer's IP address is 192.168.0.5. I need to get these had two values programmatically in order to send a broadcast message to my network (in the form 192.168.0.255, for my particular case)
许多答案没有给出我预期的结果,因为我有两个不同的网络 IP.Torial 的代码成功了(它给了我IP 地址).
Many answers were not giving the results I expected because I had two different network IP's. Torial's code did the trick (it gave me both IP addresses).
编辑 2:感谢 Brian R. Bondy有关子网掩码的信息.
Edit 2: Thanks to Brian R. Bondy for the info about the subnet mask.
这个问题比看起来更棘手,因为在许多情况下,没有本地计算机的 IP 地址",而是许多不同的 IP地址.例如,我现在正在使用的 Mac(这是一个非常基本的标准 Mac 设置)具有以下关联的 IP 地址:
The question is trickier than it appears, because in many cases there isn't "an IP address for the local computer" so much as a number of different IP addresses. For example, the Mac I'm typing on right now (which is a pretty basic, standard Mac setup) has the following IP addresses associated with it:
fe80::1%lo0
127.0.0.1
::1
fe80::21f:5bff:fe3f:1b36%en1
10.0.0.138
172.16.175.1
192.168.27.1
...而且这不仅仅是弄清楚上面哪个是真实IP地址"的问题,或者...它们都是真实的"和有用的;一些比其他更有用,具体取决于您将使用这些地址的目的.
... and it's not just a matter of figuring out which of the above is "the real IP address", either... they are all "real" and useful; some more useful than others depending on what you are going to use the addresses for.
根据我的经验,为本地计算机获取IP 地址"的最佳方法通常根本不是查询本地计算机,而是询问您的程序正在与之通信的计算机,它认为您的计算机的 IP 地址为.例如如果您正在编写客户端程序,请向服务器发送一条消息,要求服务器将您的请求所来自的 IP 地址作为数据发回.这样一来,您就会知道相关的 IP 地址是什么,前提是您正在与之通信的计算机的上下文.
In my experience often the best way to get "an IP address" for your local computer is not to query the local computer at all, but rather to ask the computer your program is talking to what it sees your computer's IP address as. e.g. if you are writing a client program, send a message to the server asking the server to send back as data the IP address that your request came from. That way you will know what the relevant IP address is, given the context of the computer you are communicating with.
也就是说,该技巧可能不适用于某些目的(例如,当您不与特定计算机通信时),因此有时您只需要收集与您的机器相关联的所有 IP 地址的列表.在 Unix/Mac (AFAIK) 下执行此操作的最佳方法是调用 getifaddrs() 并迭代结果.在 Windows 下,尝试 GetAdaptersAddresses() 以获得类似的功能.例如两者的用法,请参阅此文件中的 GetNetworkInterfaceInfos() 函数一>.
That said, that trick may not be appropriate for some purposes (e.g. when you're not communicating with a particular computer) so sometimes you just need to gather the list of all the IP addresses associated with your machine. The best way to do that under Unix/Mac (AFAIK) is by calling getifaddrs() and iterating over the results. Under Windows, try GetAdaptersAddresses() to get similar functionality. For example usages of both, see the GetNetworkInterfaceInfos() function in this file.
这篇关于如何获取本地计算机的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!