struct Hostent *h = gethostbyname (Pdomain);
Today is writing a download online file to the local applet, in the test found that gethostbyname () always return null.
Cannot parse the domain name.
What's the problem? The relevant code is copied from the original written project, the original project can work properly ah.
After more than 10 minutes of checking, finally found the reason, forgot: Winsock service initialization, WSAStartup ()
1234567891011121314151617181920212223242526 |
/*
为了在应用程序当中调用任何一个Winsock API函数,首先第一件事情就是必须通过WSAStartup函数完成对Winsock服务的初始化,
因此需要调用WSAStartup函数。使用Socket的程序在使用Socket之前必须调用WSAStartup函数。
*/
WORD wVersionRequested;
WSADATA wsaData;
int
ret;
//WinSock初始化
wVersionRequested = MAKEWORD(2, 2);
//希望使用的WinSock DLL的版本
ret = WSAStartup(wVersionRequested, &wsaData);
if
(ret != 0)
{
appLog(
"WSAStartup() failed!"
);
return
FALSE;
}
//确认WinSock DLL支持版本2.2
if
(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
appLog(
"Invalid WinSock version!"
);
WSACleanup();
return
FALSE;
}
|
2014-06-28
gethostbyname (domain) always returns NULL, messy