views:

154

answers:

1

Hi All, I have a small doubt regarding network connection procedure in Android.

My scenario is like, I am connecting to remote server over TCP using 3G. After it got connected, I enabled Wi-Fi. Android has switched to WiFi. And, still connection is alive with server, means there is no connection drop.

Here, I want to know one thing. When N/W is switched from 3G to Wi-Fi, will Android (or any other) device change its IP address as well ? and if it changes its IP, will that old connection is valid ? (means, on new IP, there should be again new connection.)

Thanks in advance.

+1  A: 

The way I understand is that the IP address will change. When the phone switches on to WiFi, it is your router that assigns the device an IP address. I suspect that Android might be restarting the IP stack (just a conjecture though) and I don't remember seeing any kind of a "handoff" mechanism to let the device have the same IP address. While it maybe theoretically possible, I am not sure how complicated it can make thing :) In addition, Android has only one network interface card, so it is not possible for it to retain both the IP addresses. If it switches to WiFi, it retains ONLY the Wifi IP address.

I did observed from my experience, a scenario like this will fail:

  • Android is connected via 3G initially
  • Android Application sends a HTTP GET to a web server
  • Android senses a better WiFi connectivity and switches from 3G

In this case, the GET request fails.

Possible solutions:

  • Try multiple times: 3 times (or a different magic number, but 3 worked for me in most cases) using a try-catch-repeat should solve most problems.
  • Use code like this to check for network connectivity and proceed from there:

    if(!isOnline()) {
      Log.e("OFFLINEERROR", "No Network Connectivity");
    }
    
    
    public boolean isOnline() {
      try {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo().isConnectedOrConnecting();         
      } catch(Exception e) {
        //Returned a null so no Internet connection!
        return false;
      }
    }
    
  • Chunk your transfers (this way even if you lose your connection, you can still finish the transfer).

Hope this helps.

Legend
Hi Legend, Thanks for the answer. You are right that the IP address will change. So, Client is starting new session altogether.My Problem for 2 sessions was, the remote server was not able to close the old session as it is not getting PIN Act. Anyway, it is different issue.Thank you very much for nice explanation.
Andhravaala