|
发表于 2005-7-11 16:28:01
|
显示全部楼层
在源码里加了注释
Post by baconer
楼上的,谢谢你!不过你这个也不行,我改了改:
int get_my_address (struct in_addr *addr)
{
struct ifreq req;
int sock;
sock = socket(AF_INET, SOCK_DGRAM, 0);
strncpy (req.ifr_name, "eth0", IFNAMSIZ);
if ( ioctl(sock, SIOCGIFADDR, &req) < 0 )
{
printf("failed to ioctl: %s\n", strerror (errno));
return 0;
}
memcpy (addr, &((struct sockaddr_in *) &req.ifr_addr)->sin_addr, sizeof (struct in_addr));
return 1;
}
int main(int argc, char *argv[])
{
/*struct in_addr *addr;
addr没有分配内存啊!
改为下面的应该行了吧*/
struct in_addr addr;
char *ip;
if(get_my_address(addr))
{
//ip=inet_ntoa(*addr);
strncpy(ip, inet_ntoa(addr), sizeof(struct in_addr));
printf("IP Address : %s\n",ip);
}
return 0;
}
编译能通过,但执行后显示:段错误,得不到结果 |
|