简体   繁体   English

获取Android网络接口IP地址

[英]Get Android network interface IP address

I am writing a wifi-direct application for Android. 我正在为Android编写一个wifi直接应用程序。 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. 由于组所有者意图功能似乎不起作用(它只是随机分配)我必须找到一种方法将客户端IP地址传输到主机。 The only address I know of is the host device which comes in the group info object that WifiP2pManager can get me. 我所知道的唯一地址是WifiP2pManager可以获取的组信息对象中的主机设备。 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. 我知道哪个设备是主机,哪个设备是客户端,所以我可以打开一个套接字连接到达或尝试连接到另一个。

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. 我需要做的是找到一种方法来转移设备的Wifi P2P(Wifi Direct)客户端的IP地址,如果'主机'设备是组所有者。 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. 它有点令人困惑,但它是如何工作的。

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. 我已经看到了从ARP表中获取IP地址的事情,但是ARP表似乎在几秒钟之后(如一分钟)和ICS上自行清除,因为wifi接口被禁用Wifi直接我甚至都没有看到arp表中的任何内容。

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. 我觉得这应该很容易,但我不是一个大的Linux用户,所以我不知道什么文件将保持网络接口配置。 Is there a way to get hte IP addresses of network interfaces? 有没有办法获得网络接口的IP地址? Or at least the Wifi P2P interface? 或者至少是Wifi P2P界面? (Note: This is not the wifi address. It's similar to the tethering address except it's Wifi Direct. WifiManager does not return this) (注意:这不是wifi地址。除了Wifi Direct之外,它类似于网络共享地址.WifiManager不会返回此信息)

Thanks, 谢谢,
Mgamerz 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);
    }

Above code should help you get the ipaddress... 上面的代码应该可以帮助你获得ipaddress ......

to get the ip address of the client who is connecting to host through a socket you may use.. clientSocket = this.serverSocket.accept(); 获取通过您可能使用的套接字连接到主机的客户端的IP地址.. clientSocket = this.serverSocket.accept(); clientSocket.getInetAddress(); clientSocket.getInetAddress();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM