android - Connection to server returns EHOSTUNREACH for all apps after switch from 3g to wifi -


i've built small android test tcp client connects ip, sends hello world, , periodically sends datetime.

to test using netcat server, in following manner:

nc -l 5000 

the client connects server through socket, , datetime shows on netcat screen correctly, either using wifi or 3g network.

if start app in 3g , leave app sending datetime on small period of time (one second interval) , change wifi network ehostunreach connections server ip, made tcp client app or other app on port (for instance browser) in wifi network error occurred.

if change 3g or wifi network no error returned, if wifi network error again. , stays way until reboot phone or leave android alone 30 minutes.

this happens in networks only, , if period of data sending small, 1 second interval or less happens every time change, if period 5 seconds happens 1 in every 4-5 changes.

the client code following

    private void test(){     asynctask<void,void,void> task = new asynctask<void,void,void>(){          @override         protected void doinbackground(void... params) {             int iterador = 0;             try {                 if(msocket!= null && !msocket.isclosed()){                     msocket.close();                 }                  if(waitnetworkconnection(getapplicationcontext(), 3))                 {                      msocket = new socket(proxy.no_proxy);                     msocket.setreuseaddress(false);                     msocket.setsotimeout(100);                     msocket.settcpnodelay(false);//false seems work better                     msocket.setkeepalive(false);                     msocket.setsolinger(false, 5);                     msocket.setoobinline(false);                        msocket.connect(new inetsocketaddress("xxx.xxx.xxx", 5000));                        printwriter out;                     outputstream stream = msocket.getoutputstream();                     outputstreamwriter outputstream = new outputstreamwriter(stream);                     bufferedwriter buff = new bufferedwriter(outputstream);                     out = new printwriter(buff, true);                       out.println("hello");                     out.println("world");                      date date;                     {                         try {                             thread.sleep(200);                              if(out.checkerror() || msocket.isoutputshutdown())                             {                                 system.out.println("some error has happed let's close socket");                                   try {                                     msocket.shutdownoutput();                                 }                                 catch(exception e){}                                 try {                                     msocket.shutdowninput();                                 }                                 catch(exception e){}                                 try {                                     msocket.close();                                 }                                 catch(exception e){}                              }else                             {                                 if(waitnetworkconnection(getapplicationcontext(), 3))                                 {                                     if(iterador > 10*5)                                     {                                         thread.sleep(5000);                                     }                                     date = new date();                                     string msg = "["+string.valueof(iterador)+"] keeping connection @ "+date.tostring();                                     system.out.println("sending: " + msg);                                     out.println(msg);                                      out.flush();                                     iterador++;                                 }                             }                         } catch (interruptedexception e) {                              e.printstacktrace();                         }                      }while(!msocket.isclosed());                  }             }             catch(exception se){                 string err = se.getmessage();                 if(null == err){                     err = se.tostring();                 }                 system.out.println(err);             }             system.out.println("socket closed");              new handler(getmainlooper()).postdelayed(new runnable() {                  @override                 public void run() {                      test();                 }             }, 5000);              return null;         }      };     task.execute(); } public static boolean waitnetworkconnection(context context, int retries) throws interruptedexception {     connectivitymanager cm = (connectivitymanager)              context.getsystemservice(context.connectivity_service);      networkinfo ni = getnetworktotest(cm);      if (ni == null || !ni.isconnected()) {         // sleep short while allow system switch connecting networks.         thread.sleep(1000);         int counter = 0;         while (counter < retries && (ni == null || (ni.isavailable() &&                  !ni.isconnected()))) {             thread.sleep(500);             ni = getnetworktotest(cm);             counter++;         }     }      return (cm.getactivenetworkinfo() != null &&              cm.getactivenetworkinfo().isconnected()); }  private static networkinfo getnetworktotest(connectivitymanager cm) {     networkinfo[] nis = cm.getallnetworkinfo();     networkinfo ni = cm.getactivenetworkinfo();      (int = 0; < nis.length; i++) {         if (nis[i].gettype() == connectivitymanager.type_wifi && nis[i].isavailable()) {             ni = nis[i];             return(ni);         }     }     return(ni); } 

this tests made on samsumg galaxy s3 (android 4.1.2) , samsung galaxy s2 (android 4.1.2). 1 network in happens router d-link 802.11g/2.4ghz


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -