| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 634 人关注过本帖
标题:gethostbyname()函数
只看楼主 加入收藏
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
结帖率:98.63%
收藏
 问题点数:0 回复次数:0 
gethostbyname()函数
在下面有一个服务器,其实它根本不是一个ftp服务器了

没事做的和我一样的初初初初初学者可以编译着看看,比自己写方便多了

https://

看到它在Client.c里面用的是gethostbyname()函数,做了些查阅

gethostbyname()有若干特征

gethostbyname()成功后会返回一个指向hostent构造体的指针

但它不能处理IPv6地址

所以再新的代码中建议使用getaddrinfo()函数

gethostbyname()失败后不会設定errno

但它会设定一个叫h_errno的global integer

并且会有一个hstrerror()的函数接受h_errno,返回一个const char *描述错误

所以错誤处理可以写为


程序代码:
 hostinfo = gethostbyname (hostname);
 if(hostinfo == NULL)
       fprintf(stderr,"gethostbyname error for host: %s: %s",\
               hostname,hstrerror(h_errno));


编译时代码有用到-D_GNU_SOURCE 选项

gcc -D_GNU_SOURCE hello.c
means the compiler will use the GNU standard of compilation, the superset of all
other standards under GNU C libraries.

并且加上这个选项之后发现utils.h里的

program_invocation_short_name

就可以使用了

可以man 一下
$ man program_invocation_short_name
#define _GNU_SOURCE
       #include <errno.h>
       extern char *program_invocation_name;
       extern char *program_invocation_short_name;

[ 本帖最后由 madfrogme 于 2012-7-22 16:07 编辑 ]
搜索更多相关主题的帖子: IPv6地址 ftp服务器 
2012-07-22 14:35
快速回复:gethostbyname()函数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017220 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved