gethostbyname (domain) always returns NULL, messy

Source: Internet
Author: User

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;    intret;    //WinSock初始化    wVersionRequested = MAKEWORD(2, 2); //希望使用的WinSock DLL的版本    ret = WSAStartup(wVersionRequested, &wsaData);    if(ret != 0)    {        appLog("WSAStartup() failed!");        returnFALSE;    }    //确认WinSock DLL支持版本2.2    if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)    {        appLog("Invalid WinSock version!");        WSACleanup();         returnFALSE;    }

 

2014-06-28

gethostbyname (domain) always returns NULL, messy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.