windows 11001 在对 getaddrinfo() 的所有调用中返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4778443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
11001 returned on all calls to getaddrinfo()
提问by Shocklanced
Having an issue connecting to a device on my network. Whenever I call getaddrinfo() it returns 11001. I have checked this with numerous different IP's in the IP_ADDRESS string (Global Var). I've checked all the non-working numbers with nslookup, and most exist there.
连接到我网络上的设备时出现问题。每当我调用 getaddrinfo() 时,它都会返回 11001。我已经用 IP_ADDRESS 字符串(全局变量)中的许多不同 IP 进行了检查。我已经使用 nslookup 检查了所有非工作号码,并且大多数都存在。
getaddrinfo-returns-always-11001-host-not-found seems to be asking a similar question, but there's no answer there.
getaddrinfo-returns-always-11001-host-not-found似乎在问一个类似的问题,但那里没有答案。
At the moment, my code is not even trying to connect to the remote device, just trying to resolve an IP. Once that works I can move on to bigger and messier problems.
目前,我的代码甚至没有尝试连接到远程设备,只是尝试解析 IP。一旦成功,我就可以处理更大、更混乱的问题。
Implementation:
执行:
int connectToDevice(char *sendbuf, char *recvbuf, SOCKET ConnectSocket)
{
WSADATA wsaData;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
struct timeval tval;
fd_set rset, wset;
int iResult;
u_long mode = -1;
//Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
//Resolve the server address and port
iResult = getaddrinfo(IP_ADDRESS, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 )
{
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next)
{
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET)
{
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
//set socket to non-blocking
iResult = ioctlsocket(ConnectSocket, FIONBIO, &mode); //if mode is set to non-zero, socket set to non-blocking.
if(iResult != NO_ERROR)
{
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR ) //if an error and not WSAEWOULDBLOCK, then close socket and try next address
{
if(WSAEWOULDBLOCK != WSAGetLastError())
{
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue; //this returns control to the For loop. I.e. if a socket error, try next address
}
else //otherwise if the error was WSAEWOULDBLOCK, then use select to check for connections.
{
FD_ZERO(&rset); //initialise fd_sets for reading and writing; both the same.
FD_SET(ConnectSocket, &rset);
wset = rset;
//set tval to timeout value
tval.tv_sec = TIMEOUT;
tval.tv_usec= 0;
//select statement
//select ignores first parameter
//select takes 3xfd_sets, read set, write set, and exception set.
//select's last parameter is timeout in the form of a timeval struct
//if return == 0, timeout occured.
//if return == SOCKET_ERROR, error occured, use WSAGetLastError to check for details.
iResult = select(ConnectSocket, &rset, &wset, NULL, &tval);
if (iResult ==0)
{
closesocket(ConnectSocket);
printf("Timeout reached, closing socket");
WSACleanup();
return 1;
}
else if(iResult == SOCKET_ERROR)
{
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
}
}
break; //Breaks out of the for loop. Will only occur if continue not executed
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET)
{
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
return 0;}
Most of this code has been taken lock and stock from the msdn website, but it all seems to look ok.
此代码的大部分已从 msdn 网站锁定和库存,但看起来一切正常。
回答by JimR
回答by Richard Lyle
I just ran into this problem as well ... getaddrinfo
and gethostbyname
are both failing with a 11001 error, but ping/nslookup are working for the same host names.
我刚刚也遇到了这个问题......getaddrinfo
并且gethostbyname
都因 11001 错误而失败,但是 ping/nslookup 正在为相同的主机名工作。
Turns out I had used the symbol server earlier, and I had symbols downloaded for all the Win32 DLL's in the same directory as my executable. Removing all the .pdb directories fixed my problem.
原来我之前使用过符号服务器,并且我在与可执行文件相同的目录中为所有 Win32 DLL 下载了符号。删除所有 .pdb 目录解决了我的问题。
My guess is that gethostbyname
or getaddrinfo
fail if you have symbols and are debugging the application.
我的猜测是,gethostbyname
或者getaddrinfo
如果你有符号和调试应用程序出现故障。
回答by Nathan Kidd
gethostbyname()
will always return WSAHOST_NOT_FOUND
(0x11001) if your environment block is empty, or missing SystemRoot
.
gethostbyname()
WSAHOST_NOT_FOUND
如果您的环境块为空或丢失,则将始终返回(0x11001) SystemRoot
。
(At a guess, it is WSAStartup()
that actually requires it, but fails silently.)
(猜测,它WSAStartup()
实际上需要它,但默默地失败了。)