1

In my Android app when closing the connection(like turning of WiFi) I'm getting an infinite number of this log message [cds] shutdowninput in read which interrupts my app and makes it do tons of unnecessary input checking, I'm using java socket programming and I tried to check for that issue by calling isInputShutdown() but got nothing, that how I am trying:

public String getServerResponse() throws Exception{

      while(true){
                  BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charset.forName("UTF-8")));
                  if(xbmc_socket.isInputShutdown()){
                     return "stopped";
                  }else{
                     //continue(and that's what it always doing)
                  }
       }
}
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118

1 Answers1

1

My problem was because I'm declaring BufferedReader as a local variable (as appears in my code in the question), and that led to an opened socket in the end of the connection either this connection ended from my side or from the other side.

Hence, to overcome this problem, I declared the BufferedReader as a global variable, which enabled me to handle it when connection closed, like closing the socket manually or doing whatever fits my app the best:

public BufferedReader in;
public String getServerResponse() throws Exception{
  while(true){
      in = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charset.forName("UTF-8")));
      // the rest of the code
   }
}  
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • 1
    Thanks! ALready solved it with http://stackoverflow.com/questions/10240694/java-socket-api-how-to-tell-if-a-connection-has-been-closed ... without the need to capture that, but still will give it a try – Rami Dabain Aug 31 '16 at 12:20
  • @RonanDejhero great, I think also you can merge the two answers together. – Muhammed Refaat Aug 31 '16 at 12:58