使用java的Android IP地址[英] Android IP address with java

本文是小编为大家收集整理的关于使用java的Android IP地址的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在编写支持多人游戏的Android视频游戏.有一个专用服务器运行,通过打开套接字来单击"多人游戏"按钮时,Android连接该服务器(这可以正常工作).服务器基本上仅充当对接系统.

客户端托管游戏时,服务器将该客户端添加到主机列表中.其他客户可以选择查看此列表,然后随后连接到该主机.这是问题所在.该服务器应该跟踪主机的IP/端口,然后其他客户端应该使用此信息与主机打开插座,然后游戏开始.我正在尝试让主机将自己的IP地址发送到服务器,以供其他客户端使用.

到目前为止,我尝试了许多方法.一个是:

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,显然对其他客户没有用.

我尝试过的另一种方法是:

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.permision.internet,android.permision.access_wifi_state,android.permision.Access_Coarse_Location,android.permision.ACCESS_NETWORK_STATE.

有人可以帮助我吗?

推荐答案

如果您只需要WiFi连接的IP,则可以将IP检索为32位整数:

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

然后,为了在点数符号中构造IP;位移动并掩盖结果:

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

android.permission.access_wifi_state在清单中需要许可.

其他推荐答案

您是否必须依靠主机来找出自己的IP地址并将其提供给服务器?如果主机打开连接并向服务器发送消息,宣布其托管游戏,那么服务器可以使用连接和消息来自的IP地址吗?这将完全避免问题.

其他推荐答案

尝试此

WifiManager wim= (WifiManager) getSystemService(WIFI_SERVICE)  ;
    List<WifiConfiguration> l=  wim.getConfiguredNetworks(); 
    WifiConfiguration wc=l.get(0); 
textview.append(  "\n"+ Formatter.formatIpAddress(wim.getConnectionInfo().getIpAddress()));

本文地址:https://www.itbaoku.cn/post/102464.html

问题描述

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.

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) {
    }

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();
            }

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.

Can anybody help me?

推荐答案

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();

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 permission will be required in the manifest.

其他推荐答案

Do you have to rely on the host to figure out its own IP address and provide this to the server? If the host opens a connection and sends a message to the server announcing that it is hosting a game, then could the server use the IP address that the connection and message came from? This would avoid the problem altogether.

其他推荐答案

try this

WifiManager wim= (WifiManager) getSystemService(WIFI_SERVICE)  ;
    List<WifiConfiguration> l=  wim.getConfiguredNetworks(); 
    WifiConfiguration wc=l.get(0); 
textview.append(  "\n"+ Formatter.formatIpAddress(wim.getConnectionInfo().getIpAddress()));