开发者

Android IP address with java

开发者 https://www.devze.com 2023-01-29 21:46 出处:网络
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 fi

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()){// &&am开发者_运维百科p; 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()));
0

精彩评论

暂无评论...
验证码 换一张
取 消