gethostname()函数的用法
来源:互联网 发布:mac口红russian red 编辑:程序博客网 时间:2024/03/29 03:24
gethostname() -- 获取进程所在机器的计算机的名字
注: -- 用域名或主机名获取IP地址,这个域名或主机名可以是本地机器的主机名/域名;也可以是远端节点的域名
#include <stdio.h>
#include <unistd.h>
int main()
{
char name[65];
gethostname(name, sizeof(name));
printf("hostname = %s\n", name);
}
编译运行
---------------------------------
# gcc test.c
# ./a.out
hostname = zxl
# uname -n
zxl
理论分析
---------------------------------
task_struct
|-----------| struct nsproxy
| | -+-->|------------|
| | -| | count |
| | -| | nslock | struct uts_namespace
| | -| | *uts_ns |---->|---------------------|
|-----------| -| | *ipc_ns | | kref |
| *nsproxy -|--+ | *namespace | |---------------------|
|-----------| |------------| | char sysname[65] |
| | | char nodename[65] |
| | | char release[65] |
| | struct new_utsname name | char version[65] |
| | | char machine[65] |
|-----------| | char domainname[65] |
|---------------------|
/usr/src/linux-2.6.19/kernel/sys.c
asmlinkage long sys_gethostname(char __user *name, int len)
{
int i, errno;
if (len < 0)
return -EINVAL;
down_read(&uts_sem);
i = 1 + strlen(utsname()->nodename);
if (i > len)
i = len;
errno = 0;
if (copy_to_user(name, utsname()->nodename, i))
errno = -EFAULT;
up_read(&uts_sem);
return errno;
}
utsname() -- 返回当前进程的new_utsname结构
/usr/src/linux-2.6.19/include/linux/utsname.h
static inline struct new_utsname *utsname(void)
{
return ¤t->nsproxy->uts_ns->name;
}
注: -- 用域名或主机名获取IP地址,这个域名或主机名可以是本地机器的主机名/域名;也可以是远端节点的域名
#include <stdio.h>
#include <unistd.h>
int main()
{
char name[65];
gethostname(name, sizeof(name));
printf("hostname = %s\n", name);
}
编译运行
---------------------------------
# gcc test.c
# ./a.out
hostname = zxl
# uname -n
zxl
理论分析
---------------------------------
task_struct
|-----------| struct nsproxy
| | -+-->|------------|
| | -| | count |
| | -| | nslock | struct uts_namespace
| | -| | *uts_ns |---->|---------------------|
|-----------| -| | *ipc_ns | | kref |
| *nsproxy -|--+ | *namespace | |---------------------|
|-----------| |------------| | char sysname[65] |
| | | char nodename[65] |
| | | char release[65] |
| | struct new_utsname name | char version[65] |
| | | char machine[65] |
|-----------| | char domainname[65] |
|---------------------|
/usr/src/linux-2.6.19/kernel/sys.c
asmlinkage long sys_gethostname(char __user *name, int len)
{
int i, errno;
if (len < 0)
return -EINVAL;
down_read(&uts_sem);
i = 1 + strlen(utsname()->nodename);
if (i > len)
i = len;
errno = 0;
if (copy_to_user(name, utsname()->nodename, i))
errno = -EFAULT;
up_read(&uts_sem);
return errno;
}
utsname() -- 返回当前进程的new_utsname结构
/usr/src/linux-2.6.19/include/linux/utsname.h
static inline struct new_utsname *utsname(void)
{
return ¤t->nsproxy->uts_ns->name;
}
- gethostname()函数的用法
- gethostname(); gethostbyname(); gethostbyaddr() 的用法
- gethostname(); gethostbyname(); gethostbyaddr() 的用法
- gethostname()函数
- 关于gethostname函数失败的问题
- 关于gethostname函数失败的问题
- 关于gethostname函数失败的问题
- gethostname() gethostbyname() getprotobyname()用法
- gethostname()函数 原理
- gethostname()函数出错,但返回success的问题解决
- gethostname
- gethostname
- 都是gethostname()惹的祸
- gethostname()与gethostbyname()的联系
- socket地址解析相关的几个函数inet_addr,inet_ntoa,gethostname,gethostbyname
- 网络编程学习笔记(gethostname函数)
- gethostname()函数出错:WSAGetLastError 10093 解决方法
- 使用gethostname()函数和gethostbyname()函数获取主机相关信息
- 不重起Windows 直接更改IP 地址的三种方法
- 测试
- 跟我一起玩Win32开发(25):监视剪贴板
- DirectX Sample-ShadowVolumn阴影技术
- 达不到预期
- gethostname()函数的用法
- Java乔晓松-httpURLConnection、URL、httpClient、httpPost、httpGet之代码详解篇
- WSAStartup()函数用法
- Mysql和Oracle数据库中的分页查询
- 面试题之线程的缓存系统
- VC++ 不重启Windows直接更改IP地址
- Android反编译APK文件
- ios20-录制音频(播放,停止,开始录制)
- OutputDebugString, NKDbgPrintfW