1hostent简介
2详细资料
1
2
3
4
5
6
7
8
|
struct
hostent {
char
*h_name;
char
**h_aliases;
int
h_addrtype;
int
h_length;
char
**h_addr_list;
#define h_addr h_addr_list[0]
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include
#include
#include
#include
#include
#include
#include
int
main(
int
argc,
char
*argv[])
{
struct
hostent *h;
if
(argc !=2)
{
fprintf
(stderr,
"usage:getip address"
);
exit
(1);
}
if
((h=gethostbyname(argv[1])) == NULL)
{
printf
(
"Calling gethostbyname with %s\n"
, host_name);
remoteHost = gethostbyname(host_name);
}
else
{
printf
(
"Calling gethostbyaddr with %s\n"
, host_name);
addr.s_addr = inet_addr(host_name);
if
(addr.s_addr == INADDR_NONE) {
printf
(
"The IPv4 address entered must be a legal address\n"
);
return
1;
}
else
remoteHost = gethostbyaddr((
char
*) &addr, 4, AF_INET);
}
if
(remoteHost == NULL) {
dwError = WSAGetLastError();
if
(dwError != 0) {
if
(dwError == WSAHOST_NOT_FOUND) {
printf
(
"Host not found\n"
);
return
1;
}
else
if
(dwError == WSANO_DATA) {
printf
(
"No data record found\n"
);
return
1;
}
else
{
printf
(
"Function failed with error: %ld\n"
, dwError);
return
1;
}
}
}
else
{
printf
(
"Function returned:\n"
);
printf
(
"\tOfficial name: %s\n"
, remoteHost->h_name);
for
(pAlias = remoteHost->h_aliases; *pAlias != 0; pAlias++) {
printf
(
"\tAlternate name #%d: %s\n"
, ++i, *pAlias);
}
printf
(
"\tAddress type: "
);
switch
(remoteHost->h_addrtype) {
case
AF_INET:
printf
(
"AF_INET\n"
);
break
;
case
AF_INET6:
printf
(
"AF_INET6\n"
);
break
;
case
AF_NETBIOS:
printf
(
"AF_NETBIOS\n"
);
break
;
default
:
printf
(
" %d\n"
, remoteHost->h_addrtype);
break
;
}
printf
(
"\tAddress length: %d\n"
, remoteHost->h_length);
if
(remoteHost->h_addrtype == AF_INET) {
while
(remoteHost->h_addr_list[i] != 0) {
addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
printf
(
"\tIPv4 Address #%d: %s\n"
, i, inet_ntoa(addr));
}
}
else
if
(remoteHost->h_addrtype == AF_INET6)
printf
(
"\tRemotehost is an IPv6 address\n"
);
}
return
0;
}
|