How to get localhost IP and gateway information ?
-
Try this: DWORD GetLocalIP() { static char sName[MAX_COMPUTERNAME_LENGTH + 1]; DWORD dwLen = sizeof(sName); GetComputerName(sName, &dwLen); HOSTENT *host = gethostbyname(sName); DWORD dwIP = 0; if(host->h_addr_list[0] != 0) { dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][0]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][1]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][2]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][3]); } return dwIP; }
-
Try this: DWORD GetLocalIP() { static char sName[MAX_COMPUTERNAME_LENGTH + 1]; DWORD dwLen = sizeof(sName); GetComputerName(sName, &dwLen); HOSTENT *host = gethostbyname(sName); DWORD dwIP = 0; if(host->h_addr_list[0] != 0) { dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][0]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][1]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][2]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][3]); } return dwIP; }
Bad assumption. You should use the ntohXX functions to convert from network format to host. You never know, one day you will write code that isn't destined to run on an x86 box. I migrated stuff between m68k, x86 and Sparc. Trust me on this, it's not fun. Steve S
-
Bad assumption. You should use the ntohXX functions to convert from network format to host. You never know, one day you will write code that isn't destined to run on an x86 box. I migrated stuff between m68k, x86 and Sparc. Trust me on this, it's not fun. Steve S
ntohXX functions are very very slow, you shouldn't use them at all, I've written an own version with does the whole job with about 10 clocks. Don't try it, just do it! ;-)
-
ntohXX functions are very very slow, you shouldn't use them at all, I've written an own version with does the whole job with about 10 clocks. Don't try it, just do it! ;-)