0

I'm programming a game in Android that uses AI, which requires big CPU power, that a normal Android device just doesn't have. So I decided to write a server in Java using sockets that will calculate everything and return a value to the client (the android device).

Now, I'm used to program for PC, but not for phones. In mobile, the IP of the device can change back and forth due to data roaming and WIFI. My question is, how do you handle a changing IP? How do you tell a new connection is the same device? Or maybe the Android device does all of that automatically?

I'm new to stackoverflow, I hope I didn't ask too many questions. :) Thank you very much for your answers!

2 Answers2

0

You don't need to handle ip changing at all. A client(an android device) must know server host/ip and reconnect if it was disconnected from network, nothing more.

private static class ConnectionTask implements Runnable {

    private boolean connected;

    @Override public void run() {
        try {
            InetAddress serverAddress = InetAddress.getByName("host");
            Socket socket = new Socket(serverAddress, 9999);
            connected = true;

            while (connected) {
                // sending or writing data
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            //here you lost the connection due to some reason
            //you need to notify user about the problem and wait for connection
        }
    }
}

To receive event about network state you need to register receiver:

context.registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (isNetworkAvailable()) {
            unregisterReceiver(this);
            tryToConnect();
        }
    }
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
eleven
  • 6,779
  • 2
  • 32
  • 52
  • Yeah but if I try to reconnect, the server will recieve a new connection request from a DIFFERENT IP. How do I handle that? How can I tell this new IP is the same user from before? – user1638851 Oct 04 '14 at 17:03
  • @user1638851 obviously you need to authenticate user. It could be login/pass or something other. But it's another question. – eleven Oct 04 '14 at 17:15
  • But I don't need any authentication. Let me explain it better. Let's say I want to make an app like Siri. You say something to Siri and she sends my memo to the Siri servers and they return an answer to the client. No authentication needed. So how can I tell a different IP is the same user if I don't authenticate my users? – user1638851 Oct 04 '14 at 17:36
  • @user1638851 iOS device could send `UUID`. – eleven Oct 04 '14 at 17:38
0

Okay guys I figured out a way, thank to Fox in socks' answer. Each time a user connects to the server socket, you take his UUID (or an hashed version of it, if you want more security :P ). Then, when that user disconnects for some reason and tries to connect to the server socket again, he'll send the same UUID. That way, you can tell both of the connections are the same, and continue with the processing.

For more information about UUID, look here: Is there a unique Android device ID?

Thank you all! :) Now how do I mark this question as a closed one? :P

Community
  • 1
  • 1
  • You need not mark a question explicitly as closed. This happens implicitly by having at least one answer. However, you should select one the answers as your preferred answer. This can be done with any level of reputation. As soon you get enough reputation to upvote you may want to revisit yoiur question and upvote other answers if applicable. – Marcus Rickert Oct 05 '14 at 23:29