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
}
}