您的位置:首页 > 编程语言 > C语言/C++

一个c++获取ip的简单例子

2013-04-18 08:41 483 查看
直接上代码:

// GetLocalIP.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

/* 编译环境: visual c++ */

#include <stdio.h>

#include <winsock2.h>

#pragma comment(lib,"ws2_32.lib")

int doit(int, char **)

{

 char host_name[255];

 //获取本地主机名称

 if (gethostname(host_name, sizeof(host_name)) == SOCKET_ERROR) {

  printf("Error %d when getting local host name.\n", WSAGetLastError());

  return 1;

 }

 printf("Host name is: %s\n", host_name);

 //从主机名数据库中得到对应的“主机”

 struct hostent *phe = gethostbyname(host_name);

 if (phe == 0) {

  printf("Yow! Bad host lookup.");

  return 1;

 }

 //循环得出本地机器所有IP地址

 for (int i = 0; phe->h_addr_list[i] != 0; ++i) {

  struct in_addr addr;

  memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));

  printf("Address %d : %s\n" , i, inet_ntoa(addr));

  unsigned long lip = inet_addr(inet_ntoa(addr));

  printf("long ip %d =", lip);

 }

 return 0;

}

int _tmain(int argc, _TCHAR* argv[])

{

 WSAData wsaData;

 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {

  return 255;

 }

 int retval = doit(argc, 0);

 WSACleanup();

 return retval;

}

 

 

linux:

编程获取IP地址大约有以下三种思路:

1. 通过gethostname()和gethostbyname()

#include <stdio.h>

#include <unistd.h>

#include <netdb.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

int main() {

    char hname[128];

    struct hostent *hent;

    int i;

    gethostname(hname, sizeof(hname));

    //hent = gethostent();

    hent = gethostbyname(hname);

    printf("hostname: %s/naddress list: ", hent->h_name);

    for(i = 0; hent->h_addr_list[i]; i++) {

        printf("%s/t", inet_ntoa(*(struct in_addr*)(hent->h_addr_list[i])));

    }

    return 0;

}

运行:

[whb@jcwkyl c]$ ./local_ip

hostname: jcwkyl.jlu.edu.cn

address list: 10.60.56.90      

2. 通过枚举网卡,API接口可查看man 7 netdevice

/*代码来自StackOverflow: http://stackoverflow.com/questions/212528/linux-c-get-the-ip-address-of-local-computer */

#include <stdio.h>     

#include <sys/types.h>

#include <ifaddrs.h>

#include <netinet/in.h>

#include <string.h>

#include <arpa/inet.h>

int main (int argc, const char * argv[]) {

    struct ifaddrs * ifAddrStruct=NULL;

    void * tmpAddrPtr=NULL;

    getifaddrs(&ifAddrStruct);

    while (ifAddrStruct!=NULL) {

        if (ifAddrStruct->ifa_addr->sa_family==AF_INET) { // check it is IP4

            // is a valid IP4 Address

            tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;

            char addressBuffer[INET_ADDRSTRLEN];

            inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);

            printf("%s IP Address %s/n", ifAddrStruct->ifa_name, addressBuffer);

        } else if (ifAddrStruct->ifa_addr->sa_family==AF_INET6) { // check it is IP6

            // is a valid IP6 Address

            tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;

            char addressBuffer[INET6_ADDRSTRLEN];

            inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);

            printf("%s IP Address %s/n", ifAddrStruct->ifa_name, addressBuffer);

        }

        ifAddrStruct=ifAddrStruct->ifa_next;

    }

    return 0;

}

运行 :

[whb@jcwkyl c]$ ./local_ip2

lo IP Address 127.0.0.1

eth0 IP Address 10.60.56.90

eth0:1 IP Address 192.168.1.3

lo IP Address ::

eth0 IP Address ::2001:da8:b000:6213:20f:1fff

eth0 IP Address 0:0:fe80::20f:1fff

3. 打开一个对外界服务器的网络连接,通过getsockname()反查自己的IP

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: