免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2462 | 回复: 1
打印 上一主题 下一主题

[函数] 菜鸟问题!关于getaddrinfo()--解决! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-30 01:51 |只看该作者 |倒序浏览
函数原型

int getaddrinfo(const char *node, const char *service,
                       const struct addrinfo *hints,
                       struct addrinfo **res);

在测试smtp.mail.yahoo.com.cn这个地址的时候,不能返回正确值。

那位兄弟能说说这个函数的用法。


问题出在struct addrinfo中.

成员ai_addr是sockaddr结构,在使用const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t len)时,
第二参数addrptr在引入ai_addr时,应该把STRUCT SOCKADDR转换为STRUCT SOCKADDR_IN。

struct sockaddr_in *sa = (struct sockaddr_in *)res->ai_addr;

inet_ntop(family, &sa->sin_addr, str, len);


over~

[ 本帖最后由 loophole 于 2009-5-31 12:22 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-05-30 10:13 |只看该作者
The getaddrinfo() function performs the node name to address
     translation.   The   nodename  and  servname  arguments  are
     pointers to null-terminated strings or NULL. One or both  of
     these  arguments  must  be a non-null pointer. In the normal
     client scenario, both the nodename and servname  are  speci-
     fied.  In  the  normal server scenario, only the servname is
     specified.

     A non-null nodename string can be a node name or  a  numeric
     host  address string. The nodename can also be an IPv6 zone-
     id in the form:

     <address>%<zone-id>

     The address is the literal IPv6 link-local address  or  host
     name  of the destination. The zone-id is the interface ID of
     the IPv6 link used to  send  the  packet.  The  zone-id  can
     either  be a numeric value, indicating a literal zone value,
     or an interface name such as hme0.
     A non-null servname string can be either a service name or a
     decimal port number.

     The  caller  can  optionally  pass  an  addrinfo  structure,
     pointed  to by the hints argument, to provide hints concern-
     ing the type of socket that the caller supports.


     The addrinfo structure is defined as:

     struct addrinfo {
     int              ai_flags;      /* AI_PASSIVE, AI_CANONNAME,
                                        AI_NUMERICHOST, AI_NUMERICSERV
                                        AI_V4MAPPED, AI_ALL, AI_ADDRCONFIG */
     int              ai_family;     /* PF_xxx */
     int              ai_socktype;   /* SOCK_xxx */
     int              ai_protocol;   /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
     socklen_t        ai_addrlen;    /* length of ai_addr */
     char             *ai_canonname; /* canonical name for nodename */
     struct sockaddr  *ai_addr;      /* binary address */
     struct addrinfo  *ai_next;      /* next structure in linked list */
     };

     In this hints structure, all members  other  than  ai_flags,
     ai_family,  ai_socktype, and ai_protocol must be 0 or a null
     pointer. A value of PF_UNSPEC for ai_family  indicates  that
     the caller will accept any protocol family. A value of 0 for
     ai_socktype indicates that the caller will accept any socket
     type.   A  value  of  0  for  ai_protocol indicates that the
     caller will accept any protocol.  For example, if the caller
     handles only TCP and not UDP, then the ai_socktype member of
     the hints structure should be set to SOCK_STREAM when getad-
     drinfo()  is called. If the caller handles only IPv4 and not
     IPv6, then the  ai_family  member  of  the  hints  structure
     should  be  set  to PF_INET when getaddrinfo() is called. If
     the third argument to getaddrinfo() is a null pointer, it is
     as  if  the  caller had filled in an addrinfo structure ini-
     tialized to 0 with ai_family set to PF_UNSPEC.

     Upon success, a pointer to a linked  list  of  one  or  more
     addrinfo  structures is returned through the final argument.
     The caller can process each addrinfo structure in this  list
     by  following  the  ai_next pointer, until a null pointer is
     encountered. In each returned addrinfo structure  the  three
     members  ai_family,  ai_socktype,  and  ai_protocol  are the
     corresponding arguments for a call  to  the  socket(3SOCKET)
     function.  In  each  addrinfo  structure  the ai_addr member
     points to a filled-in socket address structure whose  length
     is specified by the ai_addrlen member.

man信息太多了,贴不完,建议还是man一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP