我是靠谱客的博主 潇洒美女,这篇文章主要介绍网络编程学习笔记(gethostname函数),现在分享给大家,希望可以做个参考。

返回当前主机的名字,原型为:

#include <unistd.h>
int gethostname(char *name, size_t namelen);

成功返回0,失败返回-1

代码如下 :

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#define BUFLEN 128


int main(int argc, char **argv)
{
	char buf[BUFLEN];
	
	if (gethostname(buf, sizeof(buf)) < 0) {
		printf("gethostname error:%sn", strerror(errno));
		return -1;
	}

	printf("hostname=%sn", buf);
	return 0;
}

输出为:

最后

以上就是潇洒美女最近收集整理的关于网络编程学习笔记(gethostname函数)的全部内容,更多相关网络编程学习笔记(gethostname函数)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(137)

评论列表共有 0 条评论

立即
投稿
返回
顶部