🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages Started by Kris Tue, 15 May 2007 08:46
gethostbyaddr() does not return any info
#1960
Author: Kris
Date: Tue, 15 May 2007 08:46
32 lines
1452 bytes
Hi,

I'm attempting to obtain the NetBIOS name of given IP address (which is 
a NAS device in my LAN).

However, the gethostbyaddr() call always returns NULL and the 
WSAGetLastError() returns 6 (=WSA_INVALID_HANDLE).

Does anybody know what I'm doing wrong ? The code below shows following 
output:

Address=1711280300
RemoteHost=00000000
Error=6

struct hostent* RemoteHost;
unsigned int Address;
char host_name[]="172.16.0.102";

Address=inet_addr(host_name);
DEBUG("Address=%d",Address);

RemoteHost=gethostbyaddr((char*)&Address,4,AF_INET);
DEBUG("RemoteHost=%08X",RemoteHost);

int Error=WSAGetLastError();
DEBUG("Error=%d",Error);


Thanks,

Kris
Re: gethostbyaddr() does not return any info
#1961
Author: Hans Galema
Date: Tue, 15 May 2007 09:35
36 lines
1907 bytes
Kris wrote:

> Does anybody know what I'm doing wrong ? 

You forgot to initialise.

The Windows Sockets WSAStartup function initiates use of the Windows Sockets DLL by a process

> char host_name[]="172.16.0.102";

How terrible to call an ip-address a hostname!

Add some code before and afther what you already have:

   WORD wVersionRequested =MAKEWORD( 1,1);
   WSADATA wsaData;
   long res=WSAStartup( wVersionRequested, &wsaData );
   if(res)
      {
      ShowMessage ( "res so return;" );
      return;
      }

   char ipaddr [] = "172.16.0.102";

   unsigned long inaddr = inet_addr ( ipaddr );

   hostent *myhostent =  gethostbyaddr( (const char FAR *)&inaddr, sizeof ( in_addr ), 0 );

   if ( myhostent ) ShowMessage ( ipaddr + AnsiString ( "\n\n" ) + myhostent->h_name );
   else
      ShowMessage ( ipaddr + AnsiString ( "\n\n" ) + "no myhostent" );

   WSACleanup();

Hans.
Re: gethostbyaddr() does not return any info
#1962
Author: Kris
Date: Tue, 15 May 2007 10:44
14 lines
1376 bytes
Hans Galema wrote:
 > Add some code before and afther what you already have:

Cool. After I changed one little thing in your example code I got it 
working perfectly now. I changed the
gethostbyaddr(..., 0 );
into
gethostbyaddr(..., AF_INET );

Thank you very much for your help.

Regards,

Kris
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads