Possible Errors in Defensive Programming

Source: Internet
Author: User

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;
}

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.