获取Android网络接口的IP地址 [英] Get Android network interface IP address

查看:552
本文介绍了获取Android网络接口的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Android编写一个wifi-direct应用程序.我正在尝试与设备建立套接字连接.由于组所有者意图功能似乎不起作用(它只是随机分配,它似乎出现了),我必须找到一种将客户端IP地址传输到主机的方法.我唯一知道的地址是WifiP2pManager可以获取的组信息对象中的主机设备.我知道哪个设备是主机,哪个设备是客户端,因此我可以打开套接字以使连接到达或尝试连接到另一设备.

I am writing a wifi-direct application for Android. I am trying to make a socket connection to a device. Since the group owner intent function doesn't seem to work (it just assigns randomly it appears) I have to find a way to transfer the clients IP address to the host. The only address I know of is the host device which comes in the group info object that WifiP2pManager can get me. I know which device is the host and which one is the client so I can open a socket for a connection to arrive or attempt to connect to the other one.

如果主机"设备是组所有者,我需要做的是找到一种方法来传输Wifi P2P(Wifi直接)客户端的设备IP地址.如果主机是组所有者,则无法连接到客户端上的套接字.有点令人困惑,但这就是它的工作原理.

What I need to do is find a way to transfer the device's IP address of the Wifi P2P (Wifi Direct) client if the 'host' device is the group owner. If the host is the group owner, I have no way to connect to the socket on the client. It's a bit confusing but that's how it works.

我见过类似的事情,例如从ARP表中获取IP地址,但是ARP表似乎仅在几秒钟后(如一分钟)就在ICS上清除了,因为wifi接口已被Wifi Direct禁用,甚至没有在arp表中看到任何东西.

I've seen things like get the IP address from the ARP table, but the ARP table seems to clear itself after only a few seconds (like a minute) and on ICS since the wifi interface is disabled for Wifi direct I don't even see anything in the arp table.

我觉得这应该很容易,但是我不是Linux的大用户,所以我不知道哪个文件可以保存网络接口配置.有没有办法获取网络接口的IP地址?或者至少是Wifi P2P接口? (注意:这不是wifi地址.除了它是Wifi Direct之外,它与网络共享地址相似.WifiManager不会返回此地址)

I feel like this should be easy but I'm not a big linux user so I don't know what file would hold the network interface configurations. Is there a way to get hte IP addresses of network interfaces? Or at least the Wifi P2P interface? (Note: This is not the wifi address. It's similar to the tethering address except it's Wifi Direct. WifiManager does not return this)

谢谢,
姆加梅尔(Mgamerz)

Thanks,
Mgamerz

推荐答案

DhcpInfo dhcpInfo = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE)).getDhcpInfo();
String ipaddress = intToIp(dhcpInfo.ipAddress)

intToIp(int integer) {
        return (integer & 0xFF) + "." + ((integer >> 8) & 0xFF) + "."
                + ((integer >> 16) & 0xFF) + "." + ((integer >> 24) & 0xFF);
    }

以上代码应该可以帮助您获取ipaddress ...

Above code should help you get the ipaddress...

获取通过套接字连接到主机的客户端的IP地址. clientSocket.getInetAddress();

to get the ip address of the client who is connecting to host through a socket you may use.. clientSocket = this.serverSocket.accept(); clientSocket.getInetAddress();

这篇关于获取Android网络接口的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆