zoukankan      html  css  js  c++  java
  • 获取IP(windows和linux)

    #ifdef _WIN32
    #include <winsock2.h>
    #include <Ws2tcpip.h>
    #pragma comment(lib,"ws2_32.lib")
    #else
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <net/if.h>
    #include <net/if_arp.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #endif

    int getselfiplist(unsigned long ipbuf[],int bufcount)
    {
    int i,count=0;
    #ifdef _WIN32
    char hostname[128];
    struct hostent* inaddrs;
    if(gethostname(hostname,128)==0)
    {
    inaddrs=gethostbyname(hostname);
    if(inaddrs)
    {
    count=inaddrs->h_length/sizeof(in_addr);
    if(count>bufcount)count=bufcount;
    for(i=0;i<count;i++)
    {
    ipbuf=*(unsigned long*)inaddrs->h_addr_list;
    }
    }
    }
    #else
    int sock;
    struct sockaddr_in sin;
    struct ifreq ifr;

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(sock>=0)//!<0
    {
    if(bufcount>100)bufcount=100;
    for(i=0;i<bufcount;i++)
    {
    sprintf(ifr.ifr_name,"eth%d",i);
    if(ioctl(sock,SIOCGIFADDR,&ifr)<0) break;
    ::memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
    ipbuf[count++]=sin.sin_addr.s_addr;
    }
    close(sock);
    }
    #endif
    return count;
    }

    #ifdef _WIN32
    #include <winsock2.h>
    #include <Ws2tcpip.h>
    #pragma comment(lib,"ws2_32.lib")
    #else
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <net/if.h>
    #include <net/if_arp.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #endif

    int getselfiplist(unsigned long ipbuf[],int bufcount)
    {
    int i,count=0;
    #ifdef _WIN32
    char hostname[128];
    struct hostent* inaddrs;
    if(gethostname(hostname,128)==0)
    {
    inaddrs=gethostbyname(hostname);
    if(inaddrs)
    {
    count=inaddrs->h_length/sizeof(in_addr);
    if(count>bufcount)count=bufcount;
    for(i=0;i<count;i++)
    {
    ipbuf=*(unsigned long*)inaddrs->h_addr_list;
    }
    }
    }
    #else
    int sock;
    struct sockaddr_in sin;
    struct ifreq ifr;

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(sock>=0)//!<0
    {
    if(bufcount>100)bufcount=100;
    for(i=0;i<bufcount;i++)
    {
    sprintf(ifr.ifr_name,"eth%d",i);
    if(ioctl(sock,SIOCGIFADDR,&ifr)<0) break;
    ::memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
    ipbuf[count++]=sin.sin_addr.s_addr;
    }
    close(sock);
    }
    #endif
    return count;
    }

  • 相关阅读:
    普林斯顿宣布开源 25 核处理器
    瑞芯微RK3399宣布系统开源,进入百余种行业市场!
    Qt浅谈之二十App自动重启及关闭子窗口
    学在LINUX下编程(各种情况比较详细)
    ASP.NET开发规范:OWIN
    IDEA14中安装go语言插件
    sqlserver不能直接create table as select
    表复制语句select into from 与 insert into select 区别鉴赏
    实现Asp.net Mvc分布式Session Redis群集
    大小端模式
  • 原文地址:https://www.cnblogs.com/yunsicai/p/3659278.html
Copyright © 2011-2022 走看看