如何获取系统的IP地址 [英] How Can I get the Ip address of System

查看:251
本文介绍了如何获取系统的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取系统的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地址 - 使用 GetAdapterAddresses 在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_ADAPTER_ADDRESSES结构中使用

由AdapterAddresses
参数指向。在Microsoft Windows
软件开发工具包(SDK)
为Windows Vista和更高版本发布
头文件的组织更改了
,并定义了SOCKET_ADDRESS
结构在Ws2def.h
头文件中,它是由Winsock2.h头文件
包含的自动
。在为
Windows Server 2003和Windows XP,
发布的Platform SDK上,SOCKET_ADDRESS结构是在Winsock2.h头文件
中声明的
。为了使用
IP_ADAPTER_ADDRESSES结构,
Winsock2.h头文件必须是
包含在Iphlpapi.h头文件
文件之前。

The SOCKET_ADDRESS structure is used in the IP_ADAPTER_ADDRESSES structure pointed to by the AdapterAddresses parameter. On the Microsoft Windows Software Development Kit (SDK) released for Windows Vista and later, the organization of header files has changed and the SOCKET_ADDRESS structure is defined in the Ws2def.h header file which is automatically included by the Winsock2.h header file. On the Platform SDK released for Windows Server 2003 and Windows XP, the SOCKET_ADDRESS structure is declared in the Winsock2.h header file. In order to use the IP_ADAPTER_ADDRESSES structure, the Winsock2.h header file must be included before the Iphlpapi.h header file.

此时,您可以致电 WSAAddressToString 字符串化 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地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆