LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-09-2009, 02:32 PM   #1
Jane2008
Member
 
Registered: Oct 2008
Posts: 36

Rep: Reputation: 15
[code]gethostbyaddr() unknown host


I need to communicate with a monitor module. The IP of it is 192.168.0.100 and the port is 8080.
The ping 192.168.0.100 works well.
The socket is created successfully but the gethostbyaddr()failed.
Would anyone help to look into it? Thanks.
int main( int argc, char **argv )
{
char buffer[4096];
struct hostent *hostaddr;
int port=8080;
struct protoent *protocol;
int rval;
int sd;
struct sockaddr_in socketaddr;
char *ptr="192.168.0.100";

protocol = getprotobyname( "tcp" );
if ( !protocol )
{ perror( "getprotobyname()" );
return (errno);
}
sd = socket( PF_INET, SOCK_STREAM, protocol->p_proto );
if ( sd == -1 )
{ perror( "socket()" );
return (errno);
}

memset( &socketaddr, 0, sizeof(socketaddr) );
socketaddr.sin_family = AF_INET;
socketaddr.sin_port = htons( port );
socketaddr.sin_addr.s_addr = inet_addr(ptr);

hostaddr = gethostbyaddr((char *)socketaddr.sin_addr.s_addr), 4, AF_INET);
if ( !hostaddr )
{ fprintf( stderr, "gethostbyname(): %s\n", hstrerror(h_errno) );
return (h_errno);
}
memcpy( &socketaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length );
rval = connect( sd, (struct sockaddr *) &socketaddr, sizeof(socketaddr) );
if ( rval == -1 )
{ perror( "connect()" );
return (errno);
}
rval = recv( sd, buffer, sizeof(buffer), 0 );
if ( rval == -1 )
{ perror( "recv()" ); }
else
{ fwrite( buffer, rval, 1, stdout ); }
close( sd );
return (0);
}
 
Old 02-09-2009, 03:49 PM   #2
Jane2008
Member
 
Registered: Oct 2008
Posts: 36

Original Poster
Rep: Reputation: 15
I tried to skip the gethostbyaddr() and use connect() directly. It still failed with timeout.
 
Old 02-10-2009, 01:41 PM   #3
Jane2008
Member
 
Registered: Oct 2008
Posts: 36

Original Poster
Rep: Reputation: 15
If i reset the monitor module, the connect() works. The problem is when the program is finished, the connection isn't released, i.e, the close() doesn't work. Actually, I add one printf under the close(), it works. I don't know why. Is there any function used to release the socket? Thanks.
 
Old 02-10-2009, 01:52 PM   #4
Jane2008
Member
 
Registered: Oct 2008
Posts: 36

Original Poster
Rep: Reputation: 15
I knew how to solve it.
shutdown() shall be used to shut down the socket.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bind9 ,host does reslove a dns name but ping says unknown host Byenary Linux - Networking 10 01-12-2011 01:33 AM
Unknown Host BH_Exeter Linux - General 8 04-05-2006 01:44 PM
i m getting the error unknown host while im pinging with the host name gunjan thakkar Linux - General 2 12-14-2004 01:19 PM
Unknown Host <Linuxmachinename> / Unable to ping by host name nishi_k_79 Linux - Networking 4 11-01-2003 01:24 PM
Unknown Host mainyeti Linux - Newbie 7 07-29-2001 06:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration