servaddr.sin_addr.s_addr 与serv.sin6_addr = in6addr_any;的负值问题

niuniu612 2005-07-13 06:47:41
servaddr.sin_addr.s_addr = htonl (INADDR_ANY); /* wildcard */

In C we cannot represent a constant structure on the right-hand side of an assignment.) To solve this problem, we write
serv.sin6_addr = in6addr_any; /* wildcard */

in6_addr的结构:
struct in6_addr {
uint8_t s6_addr[16]; /* 128-bit IPv6 address */
/* network byte ordered */
};
问题
servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
可以改写为以下吗?
servaddr.sin_addr=inaddr_any//假设inaddr_any与in6addr_any一样(注意一个有6另一个没有)

反过来:
serv.sin6_addr = in6addr_any;
可以改写为以下吗?
serv.sin6_addr.s6_addr = IN6ADDR_ANY; (假设把IN6ADDR_ANY 定义为通配符 )

--------------------------------

以下是原文
With IPv4, the wildcard address is specified by the constant INADDR_ANY, whose value is normally 0. This tells the kernel to choose the IP address. We saw the use of this in Figure 1.9 with the assignment



struct sockaddr_in servaddr;
servaddr.sin_addr.s_addr = htonl (INADDR_ANY); /* wildcard */


While this works with IPv4, where an IP address is a 32-bit value that can be represented as a simple numeric constant (0 in this case), we cannot use this technique with IPv6, since the 128-bit IPv6 address is stored in a structure. (In C we cannot represent a constant structure on the right-hand side of an assignment.) To solve this problem, we write



struct sockaddr_in6 serv;

serv.sin6_addr = in6addr_any; /* wildcard */


The system allocates and initializes the in6addr_any variable to the constant IN6ADDR_ANY_INIT. The <netinet/in.h> header contains the extern declaration for in6addr_any.

...全文
786 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,223

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧