#include
#include
int CSockio::GetIP()
{
int fd_socket;
if((fd_socket=socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
return -1;
}
struct ifreq ifr_ip;
memset(&ifr_ip, 0, sizeof(ifr_ip));
strncpy(ifr_ip.ifr_name, "eth0", sizeof(ifr_ip.ifr_name) - 1);
if(ioctl(fd_socket, SIOCGIFADDR, &ifr_ip) < 0)
{
perror("ioctl");
return -1;
}
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)&ifr_ip.ifr_addr;
close(fd_socket);
return sin->sin_addr.s_addr;
}
编译后,出错:
error:field 'ifru_addr' has incomplete type
后来看了一下if的头文件,里面定义ifru_addr时,用到结构体struct sockaddr,但此结构体是在里,于是在前面加了这个头文件,编译通过
阅读(5095) | 评论(0) | 转发(0) |