C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 05-26-2007, 06:34 AM   #1
Registered User
 
Join Date: May 2007
Posts: 2
getting ip by hostname

Hi, I have been working on this since a few hours, and I am stuck. If gethostbyaddr fails it should try gethostbyname, but neither of them is working.

Can anyone point me to the problem?


Code:
const char *host = "localhost";

int main(void) {
				char buf[128];
				LPHOSTENT hostent = NULL;
				IN_ADDR iaddr;
				DWORD addr = inet_addr(host); 

				if (addr != INADDR_NONE) {
					hostent = gethostbyaddr((char *)&addr, sizeof(struct in_addr), AF_INET);
					if (hostent != NULL) {
						sprintf(buf, ".»». -> %s.", hostent->h_name);
						puts(buf);
					}
				}
				else {
					hostent = gethostbyname(host);
					if (hostent != NULL) {
						iaddr = *((LPIN_ADDR)*hostent->h_addr_list);
						sprintf(buf, ".»». -> %s.", inet_ntoa(iaddr));
						puts(buf);
					}
				}
				if (hostent == NULL) {
					sprintf(buf,".»».  Couldn't resolve hostname.");
					puts(buf);
				}
				

	return 0;
}
NiSA is offline   Reply With Quote
Old 05-26-2007, 09:30 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,636
Read the manual pages to find out how those functions pass back extended error information, then use that to print something more informative than "it didn't work".
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 05-26-2007, 10:56 PM   #3
int x = *((int *) NULL);
 
Cactus_Hugger's Avatar
 
Join Date: Jul 2003
Location: Banks of the River Styx
Posts: 902
Quote:
If gethostbyaddr fails it should try gethostbyname
Your program's logic reads differently. If gethostbyaddr() is merely called, gethostbyname() will never get called, regardless of gethostbyaddr()'s result. Check the logic on your outermost if()/else statements. gethostbyname() gets called if inet_addr() is called, and does not return INADDR_NONE. Your implementation seems correct (much like example code out there), you understanding, however, is incorrect.

Some nitpicks: You also called sprintf/puts together - why not just printf()? Also, those fancy double arrows don't display in my (Windows) 'terminal'...

Finally, since you seem to be using windows: First, choose whether to use the windows typenames, or the standard ones. (LPHOSTENT or struct hostent *) (Use the latter - they're portable!) Finally: Are you called WSAStartup()/WSACleanup() - everything will mysteriously fail until you do. Hint: Call WSAGetLastError(), as Salem suggests:
Code:
WSAGetLastError(): 10093
...
#define WSANOTINITIALISED	(WSABASEERR+93)
__________________
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)

Last edited by Cactus_Hugger; 05-26-2007 at 11:01 PM.
Cactus_Hugger is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting 32 bit binary IP to decimal IP (vice-versa) Mankthetank19 C Programming 15 12-28-2009 07:17 PM
obtain hostname or IP afisher Networking/Device Communication 3 12-01-2004 07:46 AM
Outputting your IP and hostname Nakeerb C++ Programming 4 10-21-2002 08:24 PM
ip or hostname jchanwh C Programming 2 04-08-2002 08:41 PM


All times are GMT -6. The time now is 09:58 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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