私たちの端末デバイスの場合、wifiに接続し、デバイス名を区別したいので、ホスト名を変更することをお勧めします。ホスト名の場合は、渡すことができます
関連コンテンツを表示するmanホスト名。
現在のシステムのホスト名を確認する場合は、次のコマンドを使用できます。
(1)uname -n
(2)ホスト名
コマンドでホスト名を変更できます。
ホスト名mydefinename
指示を通じて変更するのは、実際には/ proc / sys / kernel / hostnameのコンテンツです。
同様に、システムのAPI呼び出しを使用することもできます。
コードは次のとおりです:hostname_main.cpp
#include <unistd.h>
#include <stdio.h>
#include<string.h>
int main()
{
char buf[50] = "localhost.localdomain";
if (sethostname(buf, strlen("localhost.localdomain")) < 0)
{
perror("sethostname");
}
else
{
printf("sethostname success!\n");
}
char buff[50];
if (gethostname(buff, sizeof(buff)) == 0)
{
printf("%s\n", buff);
}
else
{
perror("gethostname");
}
return 0;
}
コンパイル:
g ++ -o hostname_main hostname_main.cpp
結果は次のとおりです。
[root@localhost Test307]# ./hostname_main
sethostname success!
localhost.localdomain
[root@localhost Test307]#