Re: gethostbyname problem



You haven't shown us how chHost is declared, but the code, unless you broke
it again, is right. So, maybe you're calling a function that starts up
WinSock, runs the call, and shuts down WinSock every time, which would be
stupid and very likely to not work, or you're asking for a host name that's
wrong to be resolved, or your resolution configuration, the IP addresses of
your DNS servers, are wrong.

Write the shortest possible program that will do these steps, then run it.
If you run it a second time, it still works, right?

Paul T.

"Rajat" <raj.rr7@xxxxxxxxx> wrote in message
news:1156491103.965759.92550@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi Paul,

I tried with only one WSAStartup, But still facing the same issue. Is
there any other solution.

Thanks
Rajat


Paul G. Tobey [eMVP] wrote:
Why are you calling WSAStartup twice?! That's probably the source of all
of
your problems.

Paul T.

"Rajat" <raj.rr7@xxxxxxxxx> wrote in message
news:1156170123.066175.270980@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,

I am trying to resolve the DNS to get the IP using IP helper API
gethostbyname. The code is as follows:

hostent *hp = NULL;
struct in_addr srv_addr;
DWORD dwErr = 0;
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = 0x0101;
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return;
}

wVersionRequested = MAKEWORD(1, 1);
int nResult = WSAStartup(wVersionRequested, &wsaData);
if (nResult != 0)
{
return;
}

/* chHost contains name of the DNS to resolve*/
hp = gethostbyname(chHost);
dwErr = WSAGetLastError();
if (hp != NULL)
{
strcpy(chHostIP,inet_ntoa( *( struct in_addr*)( hp ->
h_addr_list[0])));
/* Printing the IP address*/
}

WSACleanup();

In this case for the very first time DNS is resolved and IP is given.
But when try to use the same thing for the second time gethostbyname
returns error WSANOTINITIALISED (Though WSAStartUp function is executed
successfully)

Does anybody ever find same behavior. Can anybody guide me what is
going wrong here.

Thanks in advance
Rajat




.



Relevant Pages

  • Re: gethostbyname problem
    ... I tried with only one WSAStartup, But still facing the same issue. ... Paul G. Tobey wrote: ... WSADATA wsaData; ... In this case for the very first time DNS is resolved and IP is given. ...
    (microsoft.public.windowsce.embedded)
  • Re: gethostbyname problem
    ... For some reasons, gethostbyname() doesn't function ... WSADATA wsaData; ... What exactly is in this chHost string and what does it contain? ... But when try to use the same thing for the second time gethostbyname ...
    (microsoft.public.windowsce.embedded.vc)
  • RE: [fw-wiz] Equifax Canada
    ... You actually got the ratio reversed Paul. ... "For the second time in about a year, the credit reporting company ... Equifax Canada Inc. has suffered a security breach that has given ...
    (Firewall-Wizards)
  • Re: Scumbags who checkraise twice in one hand
    ... for a check raise the second time is your own fault, fool me once... ... He should not think that I would be stupid ... Paul made a mistake in not realizing the guy was stupid.  ...
    (rec.gambling.poker)
  • Re: Does Brokeback Mountain have a GAY AGENDA?
    ... Paul of El Cajon wrote: ... That is the second time I have seen a reference to Craigslist. ...
    (rec.arts.movies.current-films)