当前位置: 技术问答>linux和unix
大家帮我看看这段取IP地址的代码?
来源: 互联网 发布时间:2015-07-30
本文导语: int GetIPAddress(std::vector &addrlist) { addrlist.clear(); char szBuff[256]; if(gethostname(szBuff, sizeof(szBuff)) != 0) { printf("Cannot get host namen"); return 0; } struct hostent * pAddrInfo = gethostbyname(szBuff); if(!pAddrInfo->h_addr_list[0]) { p...
int GetIPAddress(std::vector &addrlist)
{
addrlist.clear();
char szBuff[256];
if(gethostname(szBuff, sizeof(szBuff)) != 0)
{
printf("Cannot get host namen");
return 0;
}
struct hostent * pAddrInfo = gethostbyname(szBuff);
if(!pAddrInfo->h_addr_list[0])
{
printf("Cannot get network address!n");
return 0;
}
int i = 0;
string szAddr;
while(pAddrInfo->h_addr_list[i]){
szAddr = inet_ntoa(*(struct in_addr *)pAddrInfo->h_addr_list[i]);
addrlist.push_back(szAddr);
++i;
}
return addrlist.size();
}
为什么只能取到127。0。0。1阿
{
addrlist.clear();
char szBuff[256];
if(gethostname(szBuff, sizeof(szBuff)) != 0)
{
printf("Cannot get host namen");
return 0;
}
struct hostent * pAddrInfo = gethostbyname(szBuff);
if(!pAddrInfo->h_addr_list[0])
{
printf("Cannot get network address!n");
return 0;
}
int i = 0;
string szAddr;
while(pAddrInfo->h_addr_list[i]){
szAddr = inet_ntoa(*(struct in_addr *)pAddrInfo->h_addr_list[i]);
addrlist.push_back(szAddr);
++i;
}
return addrlist.size();
}
为什么只能取到127。0。0。1阿
|
#include
#include
#include
#include
#include
//#include
#include
#include
#include
#ifdef SOLARIS
#include
#endif
#define MAXINTERFACES 16
int
main (int argc, char *argv[])
{
register int fd, intrface, retn = 0;
struct ifreq buf[MAXINTERFACES];
struct arpreq arp;
struct ifconf ifc;
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
{
ifc.ifc_len = sizeof buf;
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))
{
intrface = ifc.ifc_len / sizeof (struct ifreq);
printf ("interface num is intrface=%dnnn", intrface);
while (intrface-- > 0)
{
printf ("net device %sn", buf[intrface].ifr_name);
/*Jugde whether the net card status is promisc */
if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface])))
{
if (buf[intrface].ifr_flags & IFF_PROMISC)
{
puts ("the interface is PROMISC");
retn++;
}
}
else
{
char str[256];
sprintf (str, "cpm: ioctl device %s",
buf[intrface].ifr_name);
perror (str);
}
/*Jugde whether the net card status is up */
if (buf[intrface].ifr_flags & IFF_UP)
{
puts ("the interface status is UP");
}
else
{
puts ("the interface status is DOWN");
}
/*Get IP of the net card */
if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
{
puts ("IP address is:");
puts (inet_ntoa(((struct sockaddr_in *) (&buf[intrface].ifr_addr))->sin_addr));
puts ("");
//puts (buf[intrface].ifr_addr.sa_data);
}
else
{
char str[256];
sprintf (str, "cpm: ioctl device %s",
buf[intrface].ifr_name);
perror (str);
}
/* this section can't get Hardware Address,I don't know whether the reason is module driver*/
// ((struct sockaddr_in*)&arp.arp_pa)->sin_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr;
#ifdef SOLARIS
arp.arp_pa.sa_family = AF_INET;
arp.arp_ha.sa_family = AF_INET;
((struct sockaddr_in *) &arp.arp_pa)->sin_addr.s_addr =
((struct sockaddr_in *) (&buf[intrface].ifr_addr))->sin_addr.
s_addr;
if (!(ioctl (fd, SIOCGARP, (char *) &arp)))
{
puts ("HW address is:");
printf ("%02x:%02x:%02x:%02x:%02x:%02xn",
(unsigned char) arp.arp_ha.sa_data[0],
(unsigned char) arp.arp_ha.sa_data[1],
(unsigned char) arp.arp_ha.sa_data[2],
(unsigned char) arp.arp_ha.sa_data[3],
(unsigned char) arp.arp_ha.sa_data[4],
(unsigned char) arp.arp_ha.sa_data[5]);
puts ("");
puts ("");
}
#else
#if 0
/*Get HW ADDRESS of the net card */
if (!(ioctl (fd, SIOCGENADDR, (char *) &buf[intrface])))
{
puts ("HW address is:");
printf ("%02x:%02x:%02x:%02x:%02x:%02xn",
(unsigned char) buf[intrface].ifr_enaddr[0],
(unsigned char) buf[intrface].ifr_enaddr[1],
(unsigned char) buf[intrface].ifr_enaddr[2],
(unsigned char) buf[intrface].ifr_enaddr[3],
(unsigned char) buf[intrface].ifr_enaddr[4],
(unsigned char) buf[intrface].ifr_enaddr[5]);
puts ("");
puts ("");
}
#endif
if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))
{
puts ("HW address is:");
printf ("%02x:%02x:%02x:%02x:%02x:%02xn",
(unsigned char) buf[intrface].ifr_hwaddr.sa_data[0],
(unsigned char) buf[intrface].ifr_hwaddr.sa_data[1],
(unsigned char) buf[intrface].ifr_hwaddr.sa_data[2],
(unsigned char) buf[intrface].ifr_hwaddr.sa_data[3],
(unsigned char) buf[intrface].ifr_hwaddr.sa_data[4],
(unsigned char) buf[intrface].ifr_hwaddr.
sa_data[5]);
puts ("");
puts ("");
}
#endif
else
{
char str[256];
sprintf (str, "cpm: ioctl device %s",
buf[intrface].ifr_name);
perror (str);
}
}
}
else
perror ("cpm: ioctl");
}
else
perror ("cpm: socket");
close (fd);
return retn;
}
#include
#include
#include
#include
//#include
#include
#include
#include
#ifdef SOLARIS
#include
#endif
#define MAXINTERFACES 16
int
main (int argc, char *argv[])
{
register int fd, intrface, retn = 0;
struct ifreq buf[MAXINTERFACES];
struct arpreq arp;
struct ifconf ifc;
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
{
ifc.ifc_len = sizeof buf;
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))
{
intrface = ifc.ifc_len / sizeof (struct ifreq);
printf ("interface num is intrface=%dnnn", intrface);
while (intrface-- > 0)
{
printf ("net device %sn", buf[intrface].ifr_name);
/*Jugde whether the net card status is promisc */
if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface])))
{
if (buf[intrface].ifr_flags & IFF_PROMISC)
{
puts ("the interface is PROMISC");
retn++;
}
}
else
{
char str[256];
sprintf (str, "cpm: ioctl device %s",
buf[intrface].ifr_name);
perror (str);
}
/*Jugde whether the net card status is up */
if (buf[intrface].ifr_flags & IFF_UP)
{
puts ("the interface status is UP");
}
else
{
puts ("the interface status is DOWN");
}
/*Get IP of the net card */
if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
{
puts ("IP address is:");
puts (inet_ntoa(((struct sockaddr_in *) (&buf[intrface].ifr_addr))->sin_addr));
puts ("");
//puts (buf[intrface].ifr_addr.sa_data);
}
else
{
char str[256];
sprintf (str, "cpm: ioctl device %s",
buf[intrface].ifr_name);
perror (str);
}
/* this section can't get Hardware Address,I don't know whether the reason is module driver*/
// ((struct sockaddr_in*)&arp.arp_pa)->sin_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr;
#ifdef SOLARIS
arp.arp_pa.sa_family = AF_INET;
arp.arp_ha.sa_family = AF_INET;
((struct sockaddr_in *) &arp.arp_pa)->sin_addr.s_addr =
((struct sockaddr_in *) (&buf[intrface].ifr_addr))->sin_addr.
s_addr;
if (!(ioctl (fd, SIOCGARP, (char *) &arp)))
{
puts ("HW address is:");
printf ("%02x:%02x:%02x:%02x:%02x:%02xn",
(unsigned char) arp.arp_ha.sa_data[0],
(unsigned char) arp.arp_ha.sa_data[1],
(unsigned char) arp.arp_ha.sa_data[2],
(unsigned char) arp.arp_ha.sa_data[3],
(unsigned char) arp.arp_ha.sa_data[4],
(unsigned char) arp.arp_ha.sa_data[5]);
puts ("");
puts ("");
}
#else
#if 0
/*Get HW ADDRESS of the net card */
if (!(ioctl (fd, SIOCGENADDR, (char *) &buf[intrface])))
{
puts ("HW address is:");
printf ("%02x:%02x:%02x:%02x:%02x:%02xn",
(unsigned char) buf[intrface].ifr_enaddr[0],
(unsigned char) buf[intrface].ifr_enaddr[1],
(unsigned char) buf[intrface].ifr_enaddr[2],
(unsigned char) buf[intrface].ifr_enaddr[3],
(unsigned char) buf[intrface].ifr_enaddr[4],
(unsigned char) buf[intrface].ifr_enaddr[5]);
puts ("");
puts ("");
}
#endif
if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))
{
puts ("HW address is:");
printf ("%02x:%02x:%02x:%02x:%02x:%02xn",
(unsigned char) buf[intrface].ifr_hwaddr.sa_data[0],
(unsigned char) buf[intrface].ifr_hwaddr.sa_data[1],
(unsigned char) buf[intrface].ifr_hwaddr.sa_data[2],
(unsigned char) buf[intrface].ifr_hwaddr.sa_data[3],
(unsigned char) buf[intrface].ifr_hwaddr.sa_data[4],
(unsigned char) buf[intrface].ifr_hwaddr.
sa_data[5]);
puts ("");
puts ("");
}
#endif
else
{
char str[256];
sprintf (str, "cpm: ioctl device %s",
buf[intrface].ifr_name);
perror (str);
}
}
}
else
perror ("cpm: ioctl");
}
else
perror ("cpm: socket");
close (fd);
return retn;
}