对该程序中的用法感到很奇怪,请大家帮忙解释下!

软件和网站开发以及相关技术探讨
回复
cmjauto
帖子: 27
注册时间: 2013-01-23 21:46
系统: windows xp

对该程序中的用法感到很奇怪,请大家帮忙解释下!

#1

帖子 cmjauto » 2013-03-01 21:53

大家好!
本人是初学者,学习版本是Ubuntu 12.04 LTS,最近练习网络编程时发现书中一个很奇怪的问题,无法解释为什么要这样。
代码不长的,其中问题的地方我已经注明了,代码在附件中,望大家下载下来看看。
我是按照书上到例子来的,还望各位朋友解释下,谢谢了!
15.1.c
(1.29 KiB) 已下载 76 次
头像
weidongxu
帖子: 13
注册时间: 2013-02-02 10:48

Re: 对该程序中的用法感到很奇怪,请大家帮忙解释下!

#2

帖子 weidongxu » 2013-03-02 11:46

看文档的话 h_addr 是宏,就是 h_addr_list[0]。为后向兼容定义的(估计早的时候没有addr_list,只有一个addr)。


http://msdn.microsoft.com/en-us/library ... 85%29.aspx


Syntax
C++


typedef struct hostent {
char FAR *h_name;
char FAR FAR **h_aliases;
short h_addrtype;
short h_length;
char FAR FAR **h_addr_list;
} HOSTENT, *PHOSTENT, FAR *LPHOSTENT;

Members

h_name

The official name of the host (PC). If using the DNS or similar resolution system, it is the Fully Qualified Domain Name (FQDN) that caused the server to return a reply. If using a local hosts file, it is the first entry after the IPv4 address.
h_aliases

A NULL-terminated array of alternate names.
h_addrtype

The type of address being returned.
h_length

The length, in bytes, of each address.
h_addr_list

A NULL-terminated list of addresses for the host. Addresses are returned in network byte order. The macro h_addr is defined to be h_addr_list[0] for compatibility with older software.
cmjauto
帖子: 27
注册时间: 2013-01-23 21:46
系统: windows xp

Re: 对该程序中的用法感到很奇怪,请大家帮忙解释下!

#3

帖子 cmjauto » 2013-03-03 10:25

谢谢,明白了……
weidongxu 写了:看文档的话 h_addr 是宏,就是 h_addr_list[0]。为后向兼容定义的(估计早的时候没有addr_list,只有一个addr)。


http://msdn.microsoft.com/en-us/library ... 85%29.aspx


Syntax
C++


typedef struct hostent {
char FAR *h_name;
char FAR FAR **h_aliases;
short h_addrtype;
short h_length;
char FAR FAR **h_addr_list;
} HOSTENT, *PHOSTENT, FAR *LPHOSTENT;

Members

h_name

The official name of the host (PC). If using the DNS or similar resolution system, it is the Fully Qualified Domain Name (FQDN) that caused the server to return a reply. If using a local hosts file, it is the first entry after the IPv4 address.
h_aliases

A NULL-terminated array of alternate names.
h_addrtype

The type of address being returned.
h_length

The length, in bytes, of each address.
h_addr_list

A NULL-terminated list of addresses for the host. Addresses are returned in network byte order. The macro h_addr is defined to be h_addr_list[0] for compatibility with older software.
回复