我有一个树莓派,使用Python的getaddrinfo
功能时出现了名称解析问题。我查看了源代码(也许是错误的),发现问题出在C函数gethostbyaddr
上。因此,我尝试创建一个简单的测试来查看这个函数的返回值。但是,套接字编程和C语言对我来说过于复杂。我的尝试是:
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
static struct gai_afd {
int a_af;
int a_addrlen;
int a_socklen;
int a_off;
const char *a_addrany;
const char *a_loopback;
};
int main()
{
struct hostent *hp;
struct gai_afd *gai_afd;
hp = gethostbyaddr("google.com", gai_afd->a_addrlen, AF_INET);
}
使用gcc编译时会出现两个警告:
warning: useless storage class specifier in empty declaration [enabled by default]
In function ‘main’: warning: assignment makes pointer from integer without a cast [enabled by default]
运行a.out会出现“分段错误”。
我需要做出哪些改变才能使上述工作正常?
我的目标是找出为什么getaddrinfo
无法解析google.com,而ping在同一台机器上却可以正常工作。我遇到的问题在这里。