how to find local IP adress for wifi connection

525 views
Skip to first unread message

GLBasic

unread,
Jun 30, 2011, 4:07:59 AM6/30/11
to android-ndk
Hi,

I can get the IP of a remote host (e.g. google.com) perfectly, so my
wifi and permissions seems set up correctly.

But using the code below, I only get 127.0.0.1 as a local IP address.
What am I doing wrong?
Thank you for helping.



// --------------- START CODE ----------------------------------
// no idea what this does, but I think I need that.
setenv("ANDROID_DNS_MODE", "local", 1);

char alladdr[16 * MAXHOSTNAMELEN];
alladdr[0]='\0';
struct addrinfo hints, *res=NULL;
int status;

// will return "localhost"
if(gethostname(alladdr, MAXHOSTNAMELEN) == SOCKET_ERROR)
return;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // AF_INET (IPv4) or AF_INET6 (IPv6) to
force version
hints.ai_socktype = SOCK_STREAM;

if ((status = getaddrinfo(alladdr, NULL, &hints, &res)) != 0)
return;

for(struct addrinfo *p = res;p != NULL; p = p->ai_next)
{
sockaddr_in* sin = (sockaddr_in*)p->ai_addr;
sprintf_IP(tmp, &sin->sin_addr); // only one loop, with 127.0.0.1
}
freeaddrinfo(res);
// --------------- END CODE ----------------------------------

Luiz Valdetaro

unread,
Jul 9, 2011, 7:36:38 AM7/9/11
to andro...@googlegroups.com

I am having the exact same problem. Seems to be because we are trying to do name resolution and the name used is the 'hostname' setup. There must be a way to get the ip directly.... like 'ifconfig' does...

> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>

Riasat Abir

unread,
Jul 9, 2011, 8:08:18 AM7/9/11
to andro...@googlegroups.com
will it be helpful detecting the address from java and use it in native side?
or calling a java method from the native side?
--

HimHim

Luiz Valdetaro

unread,
Jul 9, 2011, 9:07:05 AM7/9/11
to andro...@googlegroups.com

Calling a java method from the native will do if it is possible to find out one's ip from java... but it must be possible from C...

> HimHim
Reply all
Reply to author
Forward
0 new messages