我正在尝试从 Beej 的网络编程书中学习网络编程。 5.1节有一段代码
/*
** showip.c -- show IP addresses for a host given on the command line
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
if (argc != 2) {
fprintf(stderr,"usage: showip hostname\n");
return 1;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
printf("IP addresses for %s:\n\n", argv[1]);
for(p = res;p != NULL; p = p->ai_next) {
void *addr;
char *ipver;
// get the pointer to the address itself,
// different fields in IPv4 and IPv6:
if (p->ai_family == AF_INET) { // IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else { // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
// convert the IP to a string and print it:
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
}
freeaddrinfo(res); // free the linked list
return 0;
}
当我在 Ubuntu 终端中编译它时,出现以下错误
showip.c: In function ‘main’:
showip.c:15:25: error: storage size of ‘hints’ isn’t known
15 | struct addrinfo hints, *res, *p;
| ^~~~~
showip.c:28:23: warning: implicit declaration of function ‘getaddrinfo’ [-Wimplicit-function-declaration]
28 | if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
| ^~~~~~~~~~~
showip.c:29:54: warning: implicit declaration of function ‘gai_strerror’; did you mean ‘strerror’? [-Wimplicit-function-declaration]
29 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
| ^~~~~~~~~~~~
| strerror
showip.c:29:48: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
29 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
| ~^ ~~~~~~~~~~~~~~~~~~~~
| | |
| | int
| char *
| %d
showip.c:35:37: error: invalid use of undefined type ‘struct addrinfo’
35 | for(p = res;p != NULL; p = p->ai_next) {
| ^~
showip.c:41:22: error: invalid use of undefined type ‘struct addrinfo’
41 | if (p->ai_family == AF_INET) { // IPv4
| ^~
showip.c:42:75: error: invalid use of undefined type ‘struct addrinfo’
42 | struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
| ^~
showip.c:46:77: error: invalid use of undefined type ‘struct addrinfo’
46 | struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
| ^~
showip.c:52:28: error: invalid use of undefined type ‘struct addrinfo’
52 | inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
| ^~
showip.c:56:9: warning: implicit declaration of function ‘freeaddrinfo’ [-Wimplicit-function-declaration]
56 | freeaddrinfo(res); // free the linked list
| ^~~~~~~~~~~~
我检查了他网站上给出的勘误表页面,但发现它没有帮助。似乎所有必需的库都包含在这里,所以我不知道错误的根源是什么。如果有人能指出,那就太好了。 谢谢
正如@stark提到的,addrinfo的存储大小未知应该有助于编译代码。
由于我没有足够的声誉来评论,关于运行时错误
getaddrinfo: Name or service not known
,您需要删除协议https://
和终止正斜杠。
如果您查看
getaddrinfo
的联机帮助页:
node specifies either a
numerical network address (for IPv4, numbers-and-dots notation as
supported by inet_aton(3); for IPv6, hexadecimal string format as
supported by inet_pton(3)), or a network hostname
我们只需要主机名,不需要协议。所以你应该按如下方式运行该程序:
$ ./a.out www.google.com
IP addresses for www.google.com:
IPv4: 142.251.163.106
...
事实上,如果您查看链接的页面,它会为您提供示例输出:
$ showip www.example.net
IP addresses for www.example.net:
IPv4: 192.0.2.88
$ showip ipv6.example.com
IP addresses for ipv6.example.com:
IPv4: 192.0.2.101
IPv6: 2001:db8:8c00:22::171