测试组提了一个奇怪的bug,一断开网络,并连接iPHone,程序就crash
于是开始分析。
无非就是加日志,找出crash函数。最后发现时使用网上的一个开源代码“etcp.h”的set_address(...)函数中crash。也就是调用gethostbyname处。
查看了一下错误码(h_errno),是HOST_NOT_FOUND。
于是网上找了个简单demo获取localhost,对比有网络连接和无网络连接状况下,前者能解析到127.0.0.1,后者直接返回HOST_NOT_FOUND。
打开host文件一看,水落石出:测试人员屏蔽了一个条目,localhost 127.0.0.1
网上看了下localhost的说明,说localhost代表本机,使用localhost的数据不用经过网卡,而直接使用127.0.0.1会经过网卡~
附上stackoverflow上的代码
#include <stdio.h>
#ifdef _WIN32
# include "winsock.h"
#else
# include <netdb.h>
# include <arpa/inet.h>
#endif
static void initialise(void)
{
#ifdef _WIN32
WSADATA data;
if (WSAStartup (MAKEWORD(1, 1), &data) != 0)
{
fputs ("Could not initialise Winsock.\n", stderr);
exit (1);
}
#endif
}
static void uninitialise (void)
{
#ifdef _WIN32
WSACleanup ();
#endif
}
int main (int argc, char *argv[])
{
struct hostent *he;
if (argc == 1)
return -1;
initialise();
he = gethostbyname (argv[1]);
if (he == NULL)
{
switch (h_errno)
{
case HOST_NOT_FOUND:
fputs ("The host was not found.\n", stderr);
break;
case NO_ADDRESS:
fputs ("The name is valid but it has no address.\n", stderr);
break;
case NO_RECOVERY:
fputs ("A non-recoverable name server error occurred.\n", stderr);
break;
case TRY_AGAIN:
fputs ("The name server is temporarily unavailable.", stderr);
break;
}
}
else
{
puts (inet_ntoa (*((struct in_addr *) he->h_addr_list[0])));
}
uninitialise ();
return he != NULL;
}
$ ./a.out stackoverflow.com
69.59.196.211