-
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
-
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
-
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
-
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);
-
thank you hammer that worked.. but.. now i realized that gethostbyaddr does not recognize irc address..
anythin i can readup for this?