java - Check if Server is up without exception -


i check if ip-port open in java android application.

this code calls:

 button.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                  retreivefeedtask check=new retreivefeedtask();                 try{                 work=(check.execute(exec, serverip, serverport)).get();                 all.settext(work);//display returned                 }                 catch(exception e)                 {e.printstacktrace(); }                 if(work.equal("xyz")                 {                     //do stuff                 }                 else                  {                                      //do other stuff                 }              }         }); 

async code:

class retreivefeedtask extends asynctask<string , string , string > {      //private exception e;     string gotback="3";      @override     protected string doinbackground(string... params) {         try{                 socket s= new socket(params[1], integer.parseint(params[2]));         s.close();         }         catch(exception e){e.printstacktrace(); return "3";}           try{                  socket socket = new socket(params[1], integer.parseint(params[2]));                   printwriter output = new printwriter(socket.getoutputstream());                     bufferedreader input=new bufferedreader(new inputstreamreader(socket.getinputstream()));                 output.println(params[0]);                 output.flush();                 string read="";                 while((read=input.readline())!=null)                 {                 gotback=read;                  }                  socket.close();                 }             catch(socketexception e){e.printstacktrace(); }             catch(exception e){e.printstacktrace();}          return gotback;     } } 

currently, button freezes when press on it...

i check if {serverip1-serverport1} accepts connections, if not need know.

i assumed call asynctask twice, first normal server , second time backup server. try this, , start asynctask specifying both servers , both ports, i.e. backgroundtask(nrmsrvaddress, nrmsrvport, bcksrvaddress, bcksrvport); use following function connect server(s): (i have not run code, it's idea)

public socket connecttoserver(string normalsrv, int nrmport, string backupsrv, int bckport) {     // let's try connect normal server first.     try     {         socket retsocket = new socket();         inetaddress addr = inetaddress.getbyname(normalsrv);         socketaddress sockaddr = new inetsocketaddress(addr, nrmport);         retsocket.connect(sockaddr, 30000); // 30 seconds timeout         return retsocket;     }     catch (exception e)     {         // can't connect normal server         e.printstacktrace();     }      // normal server down, let's try connect backup server.     try     {         socket retsocket = new socket();         inetaddress addr = inetaddress.getbyname(backupsrv);         socketaddress sockaddr = new inetsocketaddress(addr, bckport);         retsocket.connect(sockaddr, 30000); // 30 seconds timeout         return retsocket;     }     catch (exception e)     {         // can't connect backup server         e.printstacktrace();     }     // none of server available     return null; }  // then, in asynctask: @override protected string doinbackground(string... params) {      socket socket = connecttoserver(params[1], integer.parseint(params[2]), params[3], integer.parseint(params[4]));     if (socket != null)      {         try{                 printwriter output = new printwriter(socket.getoutputstream());                    bufferedreader input=new bufferedreader(new inputstreamreader(socket.getinputstream()));                output.println(params[0]);                output.flush();                string read="";                while((read=input.readline())!=null)                {                   thedata=read;                }             }         catch(exception e)         {             e.printstacktrace();         }                  {              socket.close();                                                          }         return thedata; 

oh, see, try execute asynctask in onclick event , want main thread wait until it's finished? that's not way asynctask used. should use onpostexecute method of asynctask process return value. that's why button freezes.

i can suggest following:

  1. create new thread in button's onclick event. if want go asynctask anyway, read , how works.

  2. in thread (or background process) try connect "normal server" , if it's fails try backup server.

the main idea on code posted before "connecttoserver", have parts, first 1 try connect normal server , return socket gets, instead of returning socket reading right there. if fails connect same on part of backup server.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -