java - Handle socket termination in android -
i connected device using following code.
using socket code cam perfom tasks, need perform functions when server going down. not able find suitable method please help.
edit
i want detect when server disconnected client , means after doing transactions server disconnected can disable buttons ,
void sendrequest(){ try { this.clientsocket=new socket("192.168.1.11",2000); this.os=new dataoutputstream(this.clientsocket.getoutputstream()); this.in=new datainputstream(this.clientsocket.getinputstream()); sendfirtcommand(); client t=new client(); t.start(); }catch(exception e){ e.printstacktrace(); } }// end of sendrequest my thread code
private class client extends thread{ int time; public void run(){ try{ while(true){ //if(in.read()==-1) break; int size =in.available(); if(size>0){ byte data[]=new byte[size]; in.readfully(data); string str=new string(data); // system.out.println(data); //char c[]=str.tochararray(); str=tohex(data); system.out.println(str); /* if(str.equalsignorecase("050d00e7f0e1000101581d4a1d01ff")){ system.out.println("start left 3"); } */ if(str.equalsignorecase("050d00e7f0e1000101601d4a1d01ff")){ stopall(); handler.post(new runnable() { @override public void run() { enableall(); } }); } }
try if helps
try{ while(true){ if(str.equalsignorecase("050d00e7f0e1000101601d4a1d01ff")){ stopall(); handler.post(new runnable() { @override public void run() { enableall(); } }); } } }catch(ioexception e) { handler.post(new runnable() { @override public void run() { enableall(); } }); } it seems if exception handling not there in code have posted, let me know if missing ...
Comments
Post a Comment