views:

351

answers:

4

Hi all,

I am using the gethostbyname() function in the usual manner...

iaHost.s_addr = inet_addr(lpServerName);
if (iaHost.s_addr == INADDR_NONE)
{
 // Wasn't an IP address string, assume it is a name
 lpHostEntry = 0;
 lpHostEntry = gethostbyname(lpServerName);
}

to access my web site and return information. The variable, "lpServerName", can be any host, e.g., "google.com" or the loop-back, "127.0.0.1".

My system: Windows x64 or x32 (it doesn't matter the same error occurs) running on an AMD Opteron system, XAMPP is on my machine and I use it for debugging purposes, my compiler is Microsoft Visual c++ 2005.

Here's the problem: when I use the loop-back address, the code above works fine. When I try and access a web site exterior to my computer I get the following error: "Windows has triggered a breakpoint in test_gethostbyname.exe. This may be due to a corruption of the heap, and indicates a bug in test_gethostbyname.exe or any of the DLLs it has loaded. The output window may have more diagnostic information". The program, "test_gethostbyname.exe", contains only the parts of the code from a much larger project that apply to accessing the internet. Thus, I don't think the heap is being corrupted by anything I have done. I have tried the "getaddrinfo" command with the same results. I have searched the web for any kind of information on this problem with no joy (other than segmentation faults on unix systems).

Does anyone know of any problem with this command that would cause this error to occur?

Thanks in advance,

Jay

A: 

Is lpServerName:

  • not NULL terminated
  • NULL
  • already deleted

My guess would be that it's not NULL terminated.

Perhaps inet_addr will stop even if it doens't see a NULL termination, so that's why it works when you use a normal IP dotted string.

Brian R. Bondy
+1  A: 

I see you are checking for INADDR_NONE but what about the other fail state INADDR_ANY

http://msdn.microsoft.com/en-us/library/ms738563(VS.85).aspx

What string are you passing to gethostbyname()? Odds are there is something wrong with the format of lpServerName.

Paxic
A: 

I have played with whether or not the string is null terminated and the error has not gone away. The example I have compiled is from the Microsoft description of gethostname() which I would assume is functionally correct, but I still get the error.

This morning I recompiled the example on another machine that is using the express version of visual c++ and the error does not happen. Thus, I am beginning to suspect that there might be a problem with the libraries libraries on my development machine, so I am going to reinstall the compiler and the platform SDK and see if this corrects the problem.

A: 

I have found the answer. The problem appears to be the libraries on my XP system. As I mentioned before, I compiled the program on another XP system without problem. I compiled the system on my development system with Windows 7 and Visual c++ 8 express and it worked.