zoukankan      html  css  js  c++  java
  • gethostname()、sethostname()和getdomainname()、setdomainname()函数

    (1)gethostname()、sethostname()函数,获取/设置本地主机的标准主机名

    int main (int argc, char *argv[])
    {
        char buf[50];
    
        if (gethostname(buf, sizeof(buf)) == 0)
        {   
            printf("%s\n", buf);
        }   
        else
        {   
            perror("gethostname");
        }   
    
        return 0;
    }

    程序输出:

    [root@localhost ~]# ./a.out
    localhost.localdomain
    [root@localhost ~]#

    int main (int argc, char *argv[])
    {
        char buf[50] = "localhost.localdomain";
    
        if (sethostname(buf, strlen("localhost.localdomain")) < 0)
        {   
            perror("sethostname");
        }   
        else
        {   
            printf("sethostname success!\n");
        }   
    
        return 0;
    }

    程序输出:

    [root@localhost ~]# ./a.out
    sethostname success!
    [root@localhost ~]#

    (2)getdomainname()、setdomainname()函数,获取/设置本地主机的域名

    int main (int argc, char *argv[])
    {
        char buf[50];
    
        if (getdomainname(buf, sizeof(buf)) == 0)
        {   
            printf("%s\n", buf);
        }   
        else
        {   
            perror("getdomainname");
        }   
        
        return 0;
    }

    程序输出:

    [root@localhost ~]# ./a.out
    www.robot.com
    [root@localhost ~]#

    int main (int argc, char *argv[])
    {
        char buf[50] = "www.robot.com";
    
        if (setdomainname(buf, sizeof("www.robot.com")) < 0)
        {   
            perror("setdomainname");
        }   
        else
        {   
            printf("setdomainname success!\n");
        }   
        
        return 0;
    }

    程序输出:

    [root@localhost ~]# ./a.out
    setdomainname success!
    [root@localhost ~]#

  • 相关阅读:
    627. whose 和 who's
    628. why 和why not
    629 will: 各种用法tyg
    enChapter 3 Underlying Technologiesp
    使用VIEWER.JS进行简单的图片预览
    outlook2010设置失败后重新设置
    新增和编辑clob字段
    金钱大写
    pivot 与 unpivot 函数是SQL05新提供的2个函数
    从函数到委托
  • 原文地址:https://www.cnblogs.com/Robotke1/p/3067096.html
Copyright © 2011-2022 走看看