Gethostname()函数使用实例:
注意: gethostname 获取的只是一个字符串的主机名。
可以使用gethostbyname()把主机名转换成IP地址。
源代码:
#include<unistd.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<stdio.h>
#include<errno.h>
int main()
{
charname [50];
intgethnrvl;
gethnrvl=gethostname(name,sizeof(name));
if(gethnrvl==-1)
{
perror("gethostnameerror");
_exit(-1);
}
printf("%s \n",name);
return 0;
}
运行结果截图: