Redian新闻
>
How to get local hostname under linux?
avatar
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
avatar
B*s
2
ding

part.

【在 B********s 的大作中提到】
: 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;

avatar
B*s
3
没人知道么?

【在 B********s 的大作中提到】
: ding
:
: part.

avatar
e*d
4
I think it depends on your system configuration of hostname. The API reads
just your configuration and returns. If you change the hostname to abc.cs.
mit.edu in your configuration, you might get what you want.
Actually, I am not sure of it. Maybe wrong.

part.

【在 B********s 的大作中提到】
: 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;

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。