|
发表于 2004-9-9 10:43:43
|
显示全部楼层
[code:1]
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/in.h>
main()
{
int fd_arp;
struct ifreq ifr;
char device[32];
struct sockaddr_in *sin_ptr;
int ret,i;
int p[4];
strcpy(device,"eth0");
fd_arp = socket(AF_INET, SOCK_PACKET, htons(0x0806));
strcpy(ifr.ifr_name,device);
if(fd_arp>=0)
{
ret=ioctl(fd_arp, SIOCGIFADDR, &ifr);
if(ret>=0)
{
sin_ptr = (struct sockaddr_in *)&ifr.ifr_addr;
for(i=0;i<4;i++)
{
p[i]=((sin_ptr->sin_addr.s_addr)>>(i*8))&0xff;
}
printf("ip address is:%d.%d.%d.%d\n",p[0],p[1],p[2],p[3]);
}
}
}
[/code:1] |
|