与Java的Andr​​oid IP地址 [英] Android IP address with java

查看:108
本文介绍了与Java的Andr​​oid IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Android游戏支持多人游戏。有一个专用的服务器上运行该机器人连接到点击打开一个套接字多人按钮时(这工作正常)。服务器基本上只是作为一个配对系统。

I'm writing an Android video game that supports multiplayer. There is a dedicated server running which the androids connect to when the multiplayer button is clicked by opening a socket(this works fine). The server basically just acts as a matchmaking system.

当一个客户端主机游戏,服务器补充说,客户端的主机列表。其他客户端可以选择查看该列表,并随后连接到主机。这就是问题所在。服务器应该跟踪主机的IP /端口,然后其他客户都应该使用该信息来与主机然后游戏开始打开套接字。我试图去发送它自己的IP地址,服务器等客户端后使用的主机。

When a client hosts a game, the server adds that client to the list of hosts. Other clients may choose to view this list and then subsequently connect to that host. This is where the problem is. The server is supposed to keep track of the ip/port of hosts, and then other clients are supposed to use this information to open a socket with the host and then the game starts. I'm trying to get the host to send its own IP address to server for other clients to use later.

我已经试过很多方法至今。一个是:

I have tried many methods so far. One is:

try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
    }

这将返回10.0.2.15,这显然是无用的其他客户端。

This returns 10.0.2.15, which is obviously useless for other clients.

我已经试过另一种方法是这样的:

The other method I've tried is this:

String hostName = InetAddress.getLocalHost().getHostName();
        InetAddress addrs[] = InetAddress.getAllByName(hostName);
        for (InetAddress addr: addrs) {
            System.out.println ("addr.getHostAddress() = " + addr.getHostAddress());
            System.out.println ("addr.getHostName() = " + addr.getHostName());
            System.out.println ("addr.isAnyLocalAddress() = " + addr.isAnyLocalAddress());
            System.out.println ("addr.isLinkLocalAddress() = " + addr.isLinkLocalAddress());
            System.out.println ("addr.isLoopbackAddress() = " + addr.isLoopbackAddress());
            System.out.println ("addr.isMulticastAddress() = " + addr.isMulticastAddress());
            System.out.println ("addr.isSiteLocalAddress() = " + addr.isSiteLocalAddress());
            System.out.println ("");

            if (!addr.isLoopbackAddress()){// && addr.isSiteLocalAddress()) {
                myIP = addr.getHostAddress();
            }

这将返回我在寻找,当我运行它作为一个Java应用程序的IP地址,但是当我运行它作为一个Android应用程序,这是行不通的。最后,如​​果条件是有点不满意和MYIP最终被空。请注意,我已经包含了权限:android.permission.INTERNET对,android.permission.ACCESS_WIFI_STATE,android.permission.ACCESS_COARSE_LOCATION,android.permission.ACCESS_NETWORK_STATE。

This returns the ip address that I'm looking for when I run it as a java application, but when I run it as an android application, it doesn't work. The last if condition is somehow not satisfied and myIP ends up being null. Note that I have included the permissions: android.permission.INTERNET, android.permission.ACCESS_WIFI_STATE, android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_NETWORK_STATE.

任何人可以帮助我吗?

推荐答案

如果你只需要IP的无线连接,您可以获取IP作为一个32位整数:

If you just need the IP for the Wifi connection you can retrieve the IP as a 32 bit integer:

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ip = wifiInfo.getIpAddress();

然后,为了构建IP在点分十进制;比特移位和屏蔽的结果:

Then, in order to construct the IP in dot-decimal notation; bit-shift and mask the result:

String ipString = String.format(
"%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff));

android.permission.ACCESS_WIFI_STATE权限将被要求在清单中。

android.permission.ACCESS_WIFI_STATE permission will be required in the manifest.

这篇关于与Java的Andr​​oid IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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