java - Android SocketException 'Socket is closed' closed even after socket.isClosed() is used -


i have app in use socket listen messages server, has 2 activities each own method handle messages.

when start second first, close socket listener activity , start new 1 in second activities oncreate method. however, when switch activities, java.net.socketexception: socket closed error.

public synchronized void run(){     //check if thread has been shut down     while(!this.stopped){         socket = null;         try{             //socket             socket = new datagramsocket(port);             //packet             byte[] data = new byte [1024];             datagrampacket packet = new datagrampacket(data, data.length);             if(!socket.isclosed()){                 //store data socket packet                 socket.receive(packet);                 //create string data                 string received = new string(packet.getdata(),packet.getoffset(),packet.getlength());                  //log string todo remove                 log.i("received", received);                  //get new message object handler                 message msg = commandhandler.obtainmessage();                 //store string in message                 msg.obj = received;                 //send message handler                 commandhandler.sendmessage(msg);             }          }catch (ioexception e) {             e.printstacktrace();         }finally{             if(socket != null)                 socket.close();         }     } }  /**  * close listener  */ public void shutdown(){     this.stopped = true;     if(socket != null){         socket.close();     } } 

as can see above, use !socket.isclosed() check if socket closed before receiving message

the error trace:

06-27 19:48:12.129: w/system.err(19460): java.net.socketexception: socket closed 06-27 19:48:12.129: w/system.err(19460):    @ libcore.io.posix.recvfrombytes(native method) 06-27 19:48:12.129: w/system.err(19460):    @ libcore.io.posix.recvfrom(posix.java:136) 06-27 19:48:12.129: w/system.err(19460):    @ libcore.io.blockguardos.recvfrom(blockguardos.java:164) 06-27 19:48:12.129: w/system.err(19460):    @ libcore.io.iobridge.recvfrom(iobridge.java:513) 06-27 19:48:12.129: w/system.err(19460):    @ java.net.plaindatagramsocketimpl.dorecv(plaindatagramsocketimpl.java:161) 06-27 19:48:12.129: w/system.err(19460):    @ java.net.plaindatagramsocketimpl.receive(plaindatagramsocketimpl.java:169) 06-27 19:48:12.129: w/system.err(19460):    @ java.net.datagramsocket.receive(datagramsocket.java:253) 06-27 19:48:12.129: w/system.err(19460):    @ com.android.homeservice.server.tabletlistener.run(tabletlistener.java:54) 

update

so turns out have been calling start() method of thread twice in second activity, once in oncreate , again in onstart had been left on previous version of code. thank answers anyway, , sorry if have wasted time

i'd suggest rearchitecture. keep socket handling on thread, , tell thread when should exit. when thread exits, close socket.

public synchronized void run(){     //check if thread has been shut down     socket socket = new datagramsocket(port);     while(!this.stopped){         socket = null;         try{             //socket             //packet             byte[] data = new byte [1024];                 //store data socket packet                 socket.receive(packet);                 //create string data                 string received = new string(packet.getdata(),packet.getoffset(),packet.getlength());                  //log string todo remove                 log.i("received", received);                  //get new message object handler                 message msg = commandhandler.obtainmessage();                 //store string in message                 msg.obj = received;                 //send message handler                 commandhandler.sendmessage(msg);             }          }catch (ioexception e) {             e.printstacktrace();         }finally{             if(socket != null)                 socket.close();         }     }     socket.close() }  /**  * close listener  */ public void shutdown(){     this.stopped = true;  } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -