获取IP(windows和linux)
2014-04-11 18:17
501 查看
#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;
}
#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;
}
相关文章推荐
- windows和linux下执行java程序+获取本机IP
- JAVA分别实现Windows平台和Linux平台下的ip获取
- Java 获取Windows 和 Linux 下 IP 机制
- JAVA分别实现Windows平台和Linux平台下的ip获取
- windows及Linux系统下获取IP及MAC地址的方法
- JAVA分别实现Windows平台和Linux平台下的ip获取
- java工具类,在Windows,Linux系统获取电脑的MAC地址、本地IP、电脑名
- JAVA分别实现Windows平台和Linux平台下的ip获取
- 如何获取本机内网和外网IP(windows+linux)
- 如何用QT读写IP。或者是获取并修改IP。WINDOWS和LINUX下都试过可行
- 呵呵,Windows系统下获取本机ip的方法和Linux系统下获取本机ip的方法
- 用纯Java代码根据IP获取windows和linux的MAC物理地址
- 获取本机IP的方法(Windows和Linux通用)
- Linux系统 C++ 获取网络接口,以及主机网关IP
- linux 通过命令行获取本机外网IP
- linux c/c++按规则获取网卡ip
- 获取Linux和Window本机服务器IP的方法
- 用Linux命令行获取本机外网IP地址
- Linux环境下获取当前主机的IP/MAC等信息(C/C++)
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)