首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

怎么获取本地IP地址

2013-07-09 
如何获取本地IP地址?目标:获取本机IP地址。要求:不通过SOCKET方式,gethostname,gethostbyname等。忧郁的是上

如何获取本地IP地址?
目标:获取本机IP地址。
要求:不通过SOCKET方式,gethostname,gethostbyname等。

忧郁的是上面这几个API在DLL的全局参数中执行不了。编译通过,loadLib出错。
[解决办法]

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

int main(int argc, char **argv)
{

    //-----------------------------------------
    // Declare and initialize variables
    WSADATA wsaData;
    int iResult;

    DWORD dwError;

    struct hostent *remoteHost;
    char *host_name;
    struct in_addr addr;

    // Validate the parameters
    if (argc != 2) {
        printf("usage: %s ipv4 address\n", argv[0]);
        printf("  to return the host\n");
        printf("       %s 127.0.0.1\n", argv[0]);
        return 1;
    }
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    host_name = argv[1];

// If the user input is an alpha name for the host, use gethostbyname()
// If not, get host by addr (assume IPv4)
    if (isalpha(host_name[0])) {        /* host address is a name */
        printf("Calling gethostbyname with %s\n", host_name);
        remoteHost = gethostbyname(host_name);


    } else {
        printf("Calling gethostbyaddr with %s\n", host_name);
        addr.s_addr = inet_addr(host_name);
        if (addr.s_addr == INADDR_NONE) {
            printf("The IPv4 address entered must be a legal address\n");
            return 1;
        } else
            remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
    }

    if (remoteHost == NULL) {
        dwError = WSAGetLastError();
        if (dwError != 0) {
            if (dwError == WSAHOST_NOT_FOUND) {
                printf("Host not found\n");
                return 1;
            } else if (dwError == WSANO_DATA) {
                printf("No data record found\n");
                return 1;
            } else {
                printf("Function failed with error: %ld\n", dwError);
                return 1;
            }
        }
    } else {
        printf("Function returned:\n");
        printf("\tOfficial name: %s\n", remoteHost->h_name);
        printf("\tAlternate names: %s\n", remoteHost->h_aliases);
        printf("\tAddress type: ");
        switch (remoteHost->h_addrtype) {


        case AF_INET:
            printf("AF_INET\n");
            break;
        case AF_INET6:
            printf("AF_INET\n");
            break;
        case AF_NETBIOS:
            printf("AF_NETBIOS\n");
            break;
        default:
            printf(" %d\n", remoteHost->h_addrtype);
            break;
        }
        printf("\tAddress length: %d\n", remoteHost->h_length);
        addr.s_addr = *(u_long *) remoteHost->h_addr_list[0];
        printf("\tFirst IP Address: %s\n", inet_ntoa(addr));
    }

    return 0;
}



Header
 Declared in Winsock2.h.
 
Library
 Use Ws2_32.lib.
 
DLL
 Requires Ws2_32.dll.
 

[解决办法]
就是用gethostname()/gethostbyname()来获取本机的IP地址。
[解决办法]
不用SOCKET 方式的话, 你可以从注册表中得到想要的信息。
[解决办法]
上外网,用外网提供的外网IP,用IHTMLDocument2去解析网页获得IP
[解决办法]
gethostname应该可以的啊

热点排行