![]() |
| | #1 |
| Registered User Join Date: May 2007
Posts: 2
| getting ip by hostname 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 | |
| | #2 |
| and the hat of Jobseeking 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". |
| Salem is offline | |
| | #3 | |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 902
| Quote:
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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |