使用gethostbyname_r。您传递主机名称并返回地址结构。看起来像MT的环境中,这个版本是我的崩溃使应用程序试图与gethostbyname_r来取代它。谷歌很难找到一个示例用法或任何好的文档。如何在Linux
有没有人用这个gethostbyname_r方法?有任何想法吗 ?如何使用它以及如何处理它的错误情况(如果有的话)。
使用gethostbyname_r。您传递主机名称并返回地址结构。看起来像MT的环境中,这个版本是我的崩溃使应用程序试图与gethostbyname_r来取代它。谷歌很难找到一个示例用法或任何好的文档。如何在Linux
有没有人用这个gethostbyname_r方法?有任何想法吗 ?如何使用它以及如何处理它的错误情况(如果有的话)。
的功能是利用由调用者提供的临时缓冲器。诀窍是处理ERANGE
错误。
int rc, err;
char *str_host;
struct hostent hbuf;
struct hostent *result;
while ((rc = gethostbyname_r(str_host, &hbuf, buf, len, &result, &err)) == ERANGE) {
/* expand buf */
len *= 2;
void *tmp = realloc(buf, buflen);
if (NULL == tmp) {
free(buf);
perror("realloc");
}else{
buf = tmp;
}
}
if (0 != rc || NULL == result) {
perror("gethostbyname");
}
编辑
在最近的评论光我猜你真正想要的是getaddrinfo
。
找到一个博客这个,但代码是行不通的。 http://dimitry-i.blogspot.com/2010/09/how-to-use-gethostbynamer-correct.html – harry