1

I have an android application which (over the network) communicates with a desktop server application (Both programmed in Java and GUI based). For now to test, i simply have a way to specify the socket on the android phone. However, in the future when i release the application, i want it to be able to attempt connecting to the server by itself.

Some ideas i have had so far is getting the IP address of the phone, and then looping through a class C address space on the same port and seeing if any of those sockets connect. Do you guys have anything else you can suggest?

Also, what is the best way to get the phone's IP Address in Android? I have a way but it only works on android 2.3 and lower...on ICS it completely flops and gives random gibberish. Here it is:

public String getLocalIpAddress() {
    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 (Exception ex) {
    }
    return null;
}
Mohammad Adib
  • 788
  • 6
  • 19
  • 39
  • I have also tried (on the phone) to auto connect to 255.255.255.255:18250 but my server (*:18250) apparently did not get the broadcast... – Mohammad Adib Feb 25 '12 at 18:34

2 Answers2

1

Looping over C-class network needless overkill. I wouldn't use it unless as an utter last resort. Your user may have A-class network (10.x.x.x, popular in workplaces), or B-class, because... he likes it so ;-)

The better way would be to utilise broadcast address (or multicast if you prefer, but it's a bit more work). You may implement simple discovery protocol for your application based on UDP datagrams. In such case your server listens to the broadcast port, e.g UDP 192.168.0.255:6666 (port of your choosing) and responds to any activity by sending the IP:PORT it's listening to to the requester.

Phone->Broadcast: "Is there anybody?"
Server1->Phone  : "I am here. Talk to me at 192.168.0.123:6666"
Server2->Phone  : "I am here. Talk to me at 192.168.0.124:6666"

Client can then display available servers and ask to which one the user wants to connect, then connect to TCP port provided in the answer (I assume client-server connection is TCP, but it can be anything, the idea is the same).

Bartosz Moczulski
  • 1,209
  • 9
  • 18
  • I am using TCP and managed to quickly loop through a class C address space in a matter of seconds to find a server. So i think that will be my final solution. Thanks for your input, i will keep that in mind next time i attempt a large scale client - server app. Thanks! – Mohammad Adib Feb 25 '12 at 22:05
  • @MohammadAdib: how exactly did you implement this? [They](http://codeisland.org/2012/udp-multicast-on-android/) say, that not all android devices handle multicast correctly, so did you find some ultimate solution? – infografnet Jan 15 '13 at 10:51
0

The technology you are looking for is zero configuration networking, and fortunately, there are already some third-party open-source java libraries implemented so you don't need create your own from scratch. I recommend JmDNS which support both java and android, I use it in my own android application and it works quite well.

Zero configuration networking has already been widely used in many server client android application, check out my answer here for sample code.

Community
  • 1
  • 1
yorkw
  • 40,926
  • 10
  • 117
  • 130