char eth16[16]={0};
GetDeviceIP(“eth16”, eth16)
static int GetDeviceIP(char* device, char* ipaddr)
{
int sockfd;
struct ifreq req;
if((!device)||(!ipaddr))
{
return -1;
}
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
return -1;
}
memset(&req,0,sizeof(req));
strncpy(req.ifr_name, device, sizeof(req.ifr_name)-1);
if (ioctl(sockfd, SIOCGIFADDR, &req) < 0)
{
close( sockfd );
return -1;
}
strcpy(ipaddr,inet_ntoa(((struct sockaddr_in*)&(req.ifr_addr))->sin_addr));
close( sockfd );
return 0;
}
0 意見:
張貼留言