问题描述
如何获取系统的IP地址。
How can I get the IP address of the system.
我想要的ip地址,我可以看到 ipconfig
或 / bin / ifconfig
I want the Ip address as I can see after doing ipconfig
or /bin/ifconfig
推荐答案
您的意思是IP地址 - 使用在Win32中。有示例代码。
You mean 'IP Addresses' - use GetAdapterAddresses in Win32. There is sample code there.
这是一个有点复杂,因为你第一次调用API必须看看你需要多少内存,然后调用相同的API再次与所需的内存块。然后你必须走的内存块中返回的结构列表,如示例所示。你最终会得到什么是这样的:
It's a bit convoluted because you first call the API have to see how much memory you need, and then call the same API again with the required memory block. Then you have to walk the list of returned structures in that memory block as shown in the sample. What you are eventually gong to get to is this:
此时,您可以致电字符串化 SOCKET_ADDRESS
结构中包含的IP地址,无论是IPv6还是IPv4。
At that point you can call WSAAddressToString to string-ize the IP address that's held inside the SOCKET_ADDRESS
structure, whether it's IPv6 or IPv4.
这篇关于如何获取系统的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!