How to get local hostname under linux?# Programming - 葵花宝典
B*s
1 楼
Code is as below. after I got the actural local ip information (credit to
ansel), I got a correct hostname but it was truncated at the first period.
For example, it gives "abc" instead of "abc.cs.mit.edu"
How can I get the full hostname then? Please pay attention to "my code" part.
Thanks.
fd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&ifr, 0, sizeof(ifr));
memcpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));
ioctl(fd, SIOCGIFADDR, &ifr);
sin_ptr = (struct sockaddr_in *) &ifr.ifr_addr;
myself = sin
ansel), I got a correct hostname but it was truncated at the first period.
For example, it gives "abc" instead of "abc.cs.mit.edu"
How can I get the full hostname then? Please pay attention to "my code" part.
Thanks.
fd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&ifr, 0, sizeof(ifr));
memcpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));
ioctl(fd, SIOCGIFADDR, &ifr);
sin_ptr = (struct sockaddr_in *) &ifr.ifr_addr;
myself = sin