c语言获取本地ip地址
在计算机网络中,IP地址是一个非常重要的概念,它是每台设备在互联网上的唯一标识,在C语言中,我们可以使用多种方法来获取本机的IP地址,本文将详细介绍如何在C语言中获取本机IP地址的方法。
(图片来源网络,侵删)
我们需要了解IP地址的基本概念,IP地址是一种用于标识互联网上设备的地址,它由四个字节组成,每个字节的范围是0255,IP地址可以分为两类:IPv4和IPv6,IPv4是当前广泛使用的IP地址版本,而IPv6是下一代互联网协议,它的地址长度更长,可以容纳更多的设备。
在C语言中,我们可以使用以下几种方法来获取本机的IP地址:
1、使用gethostname()和gethostbyname()函数
这是最简单也是最常用的方法,我们可以通过调用gethostname()函数获取本机的主机名,然后使用gethostbyname()函数将主机名转换为IP地址,以下是一个简单的示例:
#include <stdio.h> #include <sys/socket.h> #include <netdb.h> #include <arpa/inet.h> #include <unistd.h> int main() { char host[NI_MAXHOST]; char ip[INET_ADDRSTRLEN]; struct hostent *he; if (gethostname(host, NI_MAXHOST) == 1) { perror("gethostname"); return 1; } he = gethostbyname(host); if (he == NULL) { perror("gethostbyname"); return 1; } strncpy(ip, inet_ntoa(*((struct in_addr *)he>h_addr)), INET_ADDRSTRLEN); printf("IP address: %s ", ip); return 0; }
2、使用WSAStartup()和WSAQueryNameServer()函数(仅限Windows)
对于Windows平台,我们可以使用Winsock库中的WSAStartup()和WSAQueryNameServer()函数来获取本机的IP地址,以下是一个简单的示例:
#include <stdio.h> #include <winsock2.h> #include <ws2tcpip.h> #include <iphlpapi.h> #pragma comment(lib, "iphlpapi.lib") #pragma comment(lib, "ws2_32.lib") int main() { DWORD dwSize = 0; DWORD dwRetVal = 0; LPVOID lpAddresses = NULL; ULONG ulOutBufLen = 0; LPDWORD lpdwReturned = NULL; INTERFACE_INFO *pInterfaceInfo = NULL; ULONG ulCounter = 0; ULONG ulCurrentAddressIndex = 0; WCHAR *pszBuffer = NULL; PIP_ADAPTER_INFO pAdapterInfo = NULL; PIP_ADAPTER_INFO pNextAdapterInfo = NULL; ULONG ulOutBufLenRequired = 0; DWORD dwStatus = 0; DWORD dwRetValGetAdapters = 0; DWORD dwFlags = GAA_FLAG_INCLUDE_GATEWAYS | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAMES; LPCTSTR pszDefaultGateway = NULL; LPCTSTR pszDNSServer = NULL; LPCTSTR pszFriendlyName = NULL; LPTSTR pszAdapterDescr = NULL; LPTSTR pszAddressString = NULL; LPTSTR pszNextAddressString = NULL; LPTSTR pszAdapterName = NULL; LPTSTR pszNextAdapterName = NULL; LPTSTR pszPrimaryDNSServer = NULL; LPTSTR pszPrimaryWINSServer = NULL; LPTSTR pszSecondaryDNSServer = NULL; LPTSTR pszSecondaryWINSServer = NULL; LPTSTR pszDHCPServer = NULL; LPTSTR pszLeaseObtained = NULL; LPTSTR pszLeaseExpires = NULL; LPTSTR pszDHCPEnabled = NULL; LPTSTR pszAutoConfigurationEnabled = NULL; LPTSTR pszNetworkDefaultGateway = NULL; LPTSTR pszNetworkDNSServers = NULL; LPTSTR pszNetworkWINSServers = NULL; LPTSTR pszNetworkPrimaryDNSServer = NULL; LPTSTR pszNetworkSecondaryDNSServer = NULL; LPTSTR pszNetworkPrimaryWINSServer = NULL; LPTSTR pszNetworkSecondaryWINSServer = NULL; LPTSTR pszNetworkDHCPServer = NULL; LPTSTR pszNetworkLeaseObtained = NULL; LPTSTR pszNetworkLeaseExpires = NULL; LPTSTR pszNetworkDHCPEnabled = NULL; LPTSTR pszNetworkAutoConfigurationEnabled = NULL; LPTSTR pszNetworkDefaultGatewayMacAddr = NULL; LPTSTR pszNetworkDefaultGatewayIPAddr = NULL; LPTSTR pszNetworkDHCPServerMacAddr = NULL; LPTSTR pszNetworkDHCPServerIPAddr = NULL; HANDLE hEnumerationHandle = INVALID_HANDLE_VALUE; DWORD dwNumberOfEntriesReturned = 0; DWORD dwIndex = 0; BOOL bContinueLooping = TRUE; PIP_ADAPTER_INFO pAdapterInfoArray[10]; // Assuming a maximum of 10 adapters on the system. Change this value as needed. UINT iAdapterInfoArraySize = sizeof(pAdapterInfoArray) / sizeof(pAdapterInfoArray[0]); // Number of elements in the array. Change this value as needed. UINT iCurrentAdapterIndex = 0; // Index to keep track of the current adapter being processed. Change this value as needed. UINT iCurrentAddressIndex = 0; // Index to keep track of the current IP address being processed. Change this value for each adapter. Change this value as needed. UINT iCurrentMACAddressIndex = 0; // Index to keep track of the current MAC address being processed. Change this value for each adapter. Change this value as needed. UINT iCurrentGatewayIndex = 0; // Index to keep track of the current gateway being processed. Change this value for each adapter. Change this value as needed. UINT iCurrentDNSServerIndex = 0; // Index to keep track of the current DNS server being processed. Change this value for each adapter. Change this value as needed. UINT iCurrentWINSServerIndex = 0; // Index to keep track of the current WINS server being processed. Change this value for each adapter. Change this value as needed. UINT iCurrentDHCPServerIndex = 0; // Index to keep track of the current DHCP server being processed. Change this value for each adapter. Change this value as needed. UINT iCurrentLeaseObtainedIndex = 0; // Index to keep track of the current lease obtained time being processed. Change this value for each adapter. Change this value as needed. UINT iCurrentLeaseExpiresIndex = 0; // Index to keep track of the current lease expiration time being processed. Change this value for each adapter. Change this value as needed. UINT iCurrentDHCPEnabledIndex = 0; // Index to keep track of the current DHCP enabled status being processed. Change this value for each adapter. Change this value as needed.