UNIX环境编程--------编程实例-----Gethostname()函数使用

来源:互联网 发布:数据分析 主成分分析 编辑:程序博客网 时间:2024/04/28 07:12

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;

 

}

 

运行结果截图:

        

0 0
原创粉丝点击