How to get IP address?
-
Hi all How do I get my IP address by code? Whats the shortest way possible? I tried getenv but I found out not every computer has their IP add in their environment variables. Thanks, Jayjay
-
Hi all How do I get my IP address by code? Whats the shortest way possible? I tried getenv but I found out not every computer has their IP add in their environment variables. Thanks, Jayjay
get host name first and then get the details. The following code might be helpful: char host_name[128]; hostent* host; gethostname(host_name,128); host = gethostbyname(host_name); printf("The local host: Address : %s\n", inet_ntoa(*(struct in_addr*)host->h_addr)); Regards
-
get host name first and then get the details. The following code might be helpful: char host_name[128]; hostent* host; gethostname(host_name,128); host = gethostbyname(host_name); printf("The local host: Address : %s\n", inet_ntoa(*(struct in_addr*)host->h_addr)); Regards
Hi kcynic, the gethostname didnt fill up the host_name. Is there a requirement before using your code? Thanks, Jayjay
-
Hi kcynic, the gethostname didnt fill up the host_name. Is there a requirement before using your code? Thanks, Jayjay
-
Hi kcynic, It is working now however there is no h_addr just h_addr_list[0] in the struct of hostent, I still can't find the ipadd. Whats your advise on this? Thanks, Jayjay
-
Hi kcynic, It is working now however there is no h_addr just h_addr_list[0] in the struct of hostent, I still can't find the ipadd. Whats your advise on this? Thanks, Jayjay
Hmm, interesting. With the following code compiled and linked with wsock32, I get an output of Hostname: xpDesktop The local host: Address : 210.84.23.xxx Press any key to continue . . .
#include <windows.h>
#include <winsock2.h>
#include <stdio.h>int main()
{
char host_name[128];
hostent* host;
WORD verRequested = MAKEWORD(1,1), err; // version 1.1
WSADATA wsaData;
err = WSAStartup(verRequested, &wsaData);
gethostname(host_name,128);
printf("Hostname: %s\n", host_name);
host = gethostbyname(host_name);
printf("The local host: Address : %s\n", inet_ntoa(*(struct in_addr*)host->h_addr));
WSACleanup();
system("pause");
} -
Hi kcynic, It is working now however there is no h_addr just h_addr_list[0] in the struct of hostent, I still can't find the ipadd. Whats your advise on this? Thanks, Jayjay
in winsock2.h: struct hostent { char FAR * h_name; /* official name of host */ char FAR * FAR * h_aliases; /* alias list */ short h_addrtype; /* host address type */ short h_length; /* length of address */ char FAR * FAR * h_addr_list; /* list of addresses */ #define h_addr h_addr_list[0] /* address, for backward compat */ }; if you call WSAStartup first, you should get the result.
-
in winsock2.h: struct hostent { char FAR * h_name; /* official name of host */ char FAR * FAR * h_aliases; /* alias list */ short h_addrtype; /* host address type */ short h_length; /* length of address */ char FAR * FAR * h_addr_list; /* list of addresses */ #define h_addr h_addr_list[0] /* address, for backward compat */ }; if you call WSAStartup first, you should get the result.
Actually yes i called WSAStartup first The host name is jdeguzman however the h_addr_list[0] value is
*jdeguzman
which i dont get why it returns that instead my ip address. Thanks, Jayjay
-
Actually yes i called WSAStartup first The host name is jdeguzman however the h_addr_list[0] value is
*jdeguzman
which i dont get why it returns that instead my ip address. Thanks, Jayjay
-
yes, my result is similar to yours. and the ip result is returned by calling inet_ntoa function. what's you code look like? of course you should check all the callings successful or not. msdn will give you more details i think.
Its all working now Thanks!
-
Hi all How do I get my IP address by code? Whats the shortest way possible? I tried getenv but I found out not every computer has their IP add in their environment variables. Thanks, Jayjay
And see Getting Addresses IP informations[^] of course its more of your answer but it has good info for you. :)
-
And see Getting Addresses IP informations[^] of course its more of your answer but it has good info for you. :)
This is my function for you all:
char* GetIpAddress()
{
WSADATA wsaData;char host\_name\[128\]; hostent\* host; WORD verRequested = MAKEWORD(1,1), err; err = WSAStartup(verRequested, &wsaData); gethostname(host\_name,128); host = gethostbyname(host\_name); host->h\_addr\_list\[0\]; WSACleanup(); return inet\_ntoa(\*(struct in\_addr\*)host->h\_addr);
}