gethostbyaddr( )

This is a discussion on gethostbyaddr( ) within the Networking/Device Communication forums, part of the General Programming Boards category; Im tring to get the hostname of argv[1] but i keep getting an error gethostbyname:: unknown host Code: mysock.sin_family = ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    gethostbyaddr( )

    Im tring to get the hostname of argv[1] but i keep getting an error

    gethostbyname:: unknown host

    Code:
    mysock.sin_family = AF_INET;
         mysock.sin_port = htons( MYPORT );
         mysock.sin_addr.s_addr =  inet_addr( argv[1] );
         
         host = gethostbyaddr((const char *)&mysock.sin_addr.s_addr, sizeof( struct sockaddr_in ), AF_INET ); 
         
         if( host == NULL )
         {
            herror( "gethostbyaddr:" );
    	exit( 1 );
         }
         
         if((s_conn = connect( s_sock, ( struct sockaddr *)&mysock, sizeof mysock)) == -1 )
         {
            fprintf( stderr, "Error on SOCKET( )\n" );
            return 1;
         }
         
         printf( "Connecting to server %s\n", host->h_name  );
    any idea on how to pass this
    Only the strong survives.

  2. #2
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    You have posted code using gethostbyaddr(), but the error message indicates gethostbyname() is being used. Perhaps you have posted the wrong portion of your code?

    Either way, the error indicates that the hostname you are providing is not found in your hosts table or found in DNS.

    You can test the validity of the hostname using nslookup:
    Code:
    nslookup cprogramming.com
    If the hostname works with nslookup, but not with your code, it would seem the hostname from the command-line is being modified before it is passed to gethostbyname() (assuming it is reaching gethostbyname() at all).


    Cheers,
    Jason

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    ya it has to be my code.. cause.. i ran nslookup and it works... heh here is my hole code, see what im tring to do is.. let the users input the ipaddres( irc for now ) to connect to (argv[1] as you know that )
    but.. once i input the ip addy i get unknown host

    Code:
    #define MYPORT 6667
    #define MAXCHARS 256
    
    int main( int argc, char *argv[] )
    {
    
        struct sockaddr_in  mysock;
        struct hostent      *host;
        struct sockaddr_in  addr;
        
        int sp;
        int cc;
        int x, y;
        int len;
        int s;
        int r;
        
        char buff[MAXCHARS];
        char *msg;
         
       
        if( argv[1] == 0 )
        {
           printf( "Propper Usage: sock <ip>\n" );
           exit(1);
        } 
      
      
        if((sp = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
        {
           fprintf( stderr, "Error on SOCKET( )\n" );
           return 1;
        }
         
       
         mysock.sin_family = AF_INET;
         mysock.sin_port = htons( MYPORT );
         mysock.sin_addr.s_addr = inet_addr(  argv[1] );
         
         memset(&(mysock.sin_zero), '\0', 8);
         strncat((char *)&addr.sin_addr.s_addr, argv[1], strlen( argv[1] ));
         
         host = gethostbyaddr((const char *)&addr.sin_addr, sizeof addr , AF_INET ); 
         
         if( NULL == host )
         {
            herror( "gethostbyaddr:" );
    	exit( 1 );
         }
         
         if(( cc = connect( sp, ( struct sockaddr *)&mysock, sizeof mysock )) == -1 )
         {
            fprintf( stderr, "Error on connect( )\n" );
            exit( 1 );
         }
         
         printf( "Connecting to %s\n", host->h_name  );
       
         for( x = 0; x < MAXCHARS; x++ )
         {
            if( r = recv(sp, buff, sizeof buff, 0 ) == -1 )
    	{
    	   perror( "recv:" );
    	   exit( 1 );
    	}
    	
    	recv(sp, buff, sizeof buff, 0 );
            printf( "%s\n", buff );
    
         } 
         
         return 0;
      }
    Im not good at c sockets yet.. so please take it easy and ignor the unused variables( later use )
    thanx deckard

    btw sorry my really error is
    gethostbyaddr:: Unknown host
    Last edited by xlordt; 10-06-2003 at 01:56 PM.
    Only the strong survives.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Remove this line:
    Code:
    strncat((char *)&addr.sin_addr.s_addr, argv[1], strlen( argv[1] ));
    and alter this one:
    Code:
    From this:
    host = gethostbyaddr((const char *)&addr.sin_addr, sizeof addr , AF_INET );
    
    ... to this...
    
    host = gethostbyaddr((const void*)&mysock.sin_addr, sizeof(struct in_addr), AF_INET);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    thank you hammer that worked.. but.. now i realized that gethostbyaddr does not recognize irc address..
    anythin i can readup for this?
    Only the strong survives.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  2. gethostbyaddr is slow
    By eth0 in forum Networking/Device Communication
    Replies: 7
    Last Post: 05-18-2005, 04:16 AM
  3. gethostbyaddr() returning NULL
    By Snip in forum Networking/Device Communication
    Replies: 6
    Last Post: 02-12-2005, 04:03 PM
  4. gethostbyaddr
    By canine in forum Windows Programming
    Replies: 1
    Last Post: 10-15-2001, 09:08 AM
  5. gethostbyaddr Help Plz
    By (TNT) in forum Windows Programming
    Replies: 2
    Last Post: 08-30-2001, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21