获取主机信息时出现分段错误
高并发调用时出现这种情况,好像gethostbyname
的结果没有被缓存?
static int proxy_getaddr(char *HostName)
{
struct hostent* Host;
Host=gethostbyname(HostName);
if(!Host)
{
unsigned long int addr=inet_addr(HostName);
if(addr!=-1)
Host=gethostbyaddr((char*)addr,sizeof(addr),AF_INET);
}
...
这是核心转储:
0x00000034f40e98b1 in gethostbyaddr () from /lib64/libc.so.6
0x000000000040182c in proxy_getaddr (HostName=0x48d75be0 "stackoverflow.com")
如何解决此问题?
It happens when called at high concurrency,it seems the result of gethostbyname
is not cached ?
static int proxy_getaddr(char *HostName)
{
struct hostent* Host;
Host=gethostbyname(HostName);
if(!Host)
{
unsigned long int addr=inet_addr(HostName);
if(addr!=-1)
Host=gethostbyaddr((char*)addr,sizeof(addr),AF_INET);
}
...
Here's the core dump:
0x00000034f40e98b1 in gethostbyaddr () from /lib64/libc.so.6
0x000000000040182c in proxy_getaddr (HostName=0x48d75be0 "stackoverflow.com")
How can I fix this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
gethostbyaddr 的第一个参数应该是指向 in_addr 结构的指针,而不是指向 long 的指针。请参阅http://pubs.opengroup.org/onlinepubs/009695399/functions/gethostbyaddr。 html。
The first parameter of gethostbyaddr is supposed to be a pointer to an in_addr structure, not a pointer to a long. See http://pubs.opengroup.org/onlinepubs/009695399/functions/gethostbyaddr.html.
您的代码不正确。您正在传递
inet_addr()
的结果,将其转换为指针,但您真正想要的是它的地址:(您应该使用的其他注释
in_addr_t
而不是unsigned long int
也是正确的,但这不太可能专门导致您的问题)。Your code is incorrect. You are passing the result of
inet_addr()
, cast to a pointer, but what you actually want is the address of this:(The other comments, that you should be using
in_addr_t
instead ofunsigned long int
are correct too, but it is unlikely that this specifically is causing your problem).根据此处找到的文档,您传入了错误的价值观:
以下是引用站点中的一些示例代码:
According to the documentation found here, you are passing in the wrong values:
Here is some example code from the quoted site: