(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 ~]#

相关文章:

  • 2021-07-01
  • 2021-07-01
  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2022-02-03
猜你喜欢
  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
  • 2021-05-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案