You need to use a function to resolve domain names. You can search for a bunch of functions online: gethostbyname. So I checked the msdn and found that the hostent structure was quite troublesome. According to the instructions, I quickly wrote the relevantCode,
PHE = gethostbyname ("www.xxx.com ");
Trace ("% s", inet_ntoa (* (struct in_addr *) (Phe-> h_addr_list [0]);
However, I tried to get an error. I checked n more information on the Internet, but I still don't know where the error was. It took a long time and I was depressed...
Later, I checked the Code and whether the function call was successful, and then checked the error code. The system was dizzy, and wsasstartup was forgotten... Khan
The added code is as follows and runs successfully.
It seems that defensive programming is still necessary to always prepare for the possibility of errors, even if the probability of errors is very small, as small as the probability that malloc or new memory allocation errors occur when the memory is insufficient, you should always be prepared.
# Include "stdafx. H"
# Include "gethost. H"
# Ifdef _ debug
# Define new debug_new
# Endif
// Unique applicationProgramObject
Cwinapp theapp;
Using namespace STD;
Struct hostent * Phe;
Struct in_addr IA;
Int _ tmain (INT argc, tchar * argv [], tchar * envp [])
{
Int nretcode = 0;
// Initialize the MFC and display an error upon failure
If (! Afxwininit (: getmodulehandle (null), null,: getcommandline (), 0 ))
{
// Todo: Change the error code to meet your needs.
_ Tprintf (_ T ("Fatal error: MFC initialization failed/N "));
Nretcode = 1;
}
Else if (! Afxsocketinit () // wsastartup is implicitly called here.
{
_ Tprintf (_ T ("Fatal error: winsocks initialization failed/N "));
Nretcode = 2;
}
Else
{
PHE = gethostbyname ("www.jisuwl.com ");
If (! PHE)
{
Cout <"Get error! "<Endl;
Trace ("error code: % d", wsagetlasterror ());
Return 3;
}
// Here is actually a table, and there may be other IP addresses in the back. I didn't write it if I was too lazy.
Memcpy (& IA, (Phe-> h_addr_list) [0], sizeof (struct in_addr ));
Trace ("% s", inet_ntoa (IA ));
*/
}
Return nretcode;
}