Windows和Linux得到本机IP地址的通用函数
2004-09-10 17:48
387 查看
Windows和Linux得到本机IP地址的通用函数
//参数:
ipbuf :存放得到的IP地址列表的数组,实际大小由bufcount指定。
bufcount:想要得到的IP地址列表的个数。
#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[i]=*(unsigned long*)inaddrs->h_addr_list[i];
}
}
}
#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;
}
发表于 2004年09月02日 7:50 PM --------------------------------------------------------------
我晕!
不就是:
#ifdef _WIN32
#include <winsock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
int getselfiplist(unsigned long ipbuf[],int bufcount)
{
int i,count=0;
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[i]=*(unsigned long*)inaddrs->h_addr_list[i];
}
}
}
return count;
}
#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>
int getselfiplist(unsigned long ipbuf[],int bufcount)
{
int i,count=0;
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);
}
return count;
}
#endif
除了函数接口规范一样,其他几乎没有相同的,从代码上没什么通用性可言吧!
# 回复:Windows和Linux得到本机IP地址的通用函数 2004-09-03 7:15 AM 完美废人
这跟写两个函数有什么区别……
# 回复:Windows和Linux得到本机IP地址的通用函数 2004-09-03 8:11 AM Vietor
对完美废人:
如果将函数在windows中封装为.dll在linux封装为.so对外提供联接接口不就成通用函数了吗? :-)
//参数:
ipbuf :存放得到的IP地址列表的数组,实际大小由bufcount指定。
bufcount:想要得到的IP地址列表的个数。
#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[i]=*(unsigned long*)inaddrs->h_addr_list[i];
}
}
}
#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;
}
发表于 2004年09月02日 7:50 PM --------------------------------------------------------------
评论
# 回复:Windows和Linux得到本机IP地址的通用函数 2004-09-03 12:21 AM meteor135我晕!
不就是:
#ifdef _WIN32
#include <winsock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
int getselfiplist(unsigned long ipbuf[],int bufcount)
{
int i,count=0;
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[i]=*(unsigned long*)inaddrs->h_addr_list[i];
}
}
}
return count;
}
#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>
int getselfiplist(unsigned long ipbuf[],int bufcount)
{
int i,count=0;
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);
}
return count;
}
#endif
除了函数接口规范一样,其他几乎没有相同的,从代码上没什么通用性可言吧!
# 回复:Windows和Linux得到本机IP地址的通用函数 2004-09-03 7:15 AM 完美废人
这跟写两个函数有什么区别……
# 回复:Windows和Linux得到本机IP地址的通用函数 2004-09-03 8:11 AM Vietor
对完美废人:
如果将函数在windows中封装为.dll在linux封装为.so对外提供联接接口不就成通用函数了吗? :-)
相关文章推荐
- 用C语言得到Linux主机的IP地址
- 通过Python在windows和linux中获取当前网络IP地址的一些方法
- 获取本机ip地址,并自动区分Windows还是linux操作系统
- 用C语言得到Linux主机的IP地址
- windows或linux系统下获取多网卡的ip地址
- 用c语言得到linux本机的IP地址
- 获取本机ip地址(适用于linux、windows)
- 用C语言得到Linux本机的IP地址2
- python在windows和linux下获得本机本地ip地址方法小结
- Linux下如何得到本机所有IP地址及MAC地址。
- 用C语言得到Linux主机的IP地址
- 获取本机的IP地址(Windows和Linux兼容)
- linux/Windows中JAVA获取IP地址
- Python3 pingIP地址,Windows和Linux
- 关于VMWare安装centOs得到的IP地址与xshell连接的方法(慕课网linux课程)
- JAVA得到网卡物理地址(windows和Linux)
- 用C语言得到Linux主机的IP地址
- JAVA得到网卡物理地址(Windows和Linux)(参考)
- (转载)Linux网络编程-使用gethostbyaddr,通过ip地址,得到该ip地址的主机的信息
- linux下得到本机IP地址