0

I'm trying to use TCP with an Android app, so there are two projects, one is the server and one is the client. When I run the server and open the client, everything works fine and messages are being delivered to both sides, although when I close the app (from an emulator), it won't alert me in the console that the socket connection was closed and attempt to get another connection so when trying to re-open the app, it won't reconnect and messages won't be delivered.

So what am I really doing wrong here? I'm new to Android and TCP, so I'm sorry if this is quite a rookie question.

@Override
public void run() {
    super.run();

    running = true;

    try {
        System.out.println("S: Connecting...");

        //create a server socket. A server socket waits for requests to come in over the network.
        ServerSocket serverSocket = new ServerSocket(SERVERPORT);

        //create client socket... the method accept() listens for a connection to be made to this socket and accepts it.
        while (running) {
            Socket client = serverSocket.accept();

            try {

                //sends the message to the client
                mOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);

                //read the message received from client
                BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

                //in this while we wait to receive messages from client (it's an infinite loop)
                //this while it's like a listener for messages
                while(!client.isClosed()) {
                    String message = in.readLine();
                    if (message != null && messageListener != null) {
                        //call the method messageReceived from ServerBoard class
                        messageListener.messageReceived(message);
                    }
                }


            } catch (Exception e) {
                System.out.println("S: Error");
                e.printStackTrace();
            } finally {
                client.close();
                System.out.println("S: Done.");
            }
      }

    } catch (Exception e) {
        System.out.println("S: Error");
        e.printStackTrace();
    }

}
0

1 Answer 1

2

It would be more accurate to say that you aren't testing for disconnection correctly.

  1. Socket.isClosed() does not magically become true when the peer disconnects. So using it to control a read loop is futile. It only tells you whether you have closed this socket.
  2. readLine() returns null when the peer has disconnected, but you're treating it as just another value.

A correct loop using readLine() looks like this:

while ((line = in.readLine()) != null)
{
    // ...
}
3
  • Though, I have another question. Does my code support multiple connections simultaneously?
    – Omer Aviv
    Aug 7, 2015 at 1:37
  • No. You would need to start a new thread to handle the I/O in each accepted socket. Examples abound. See for example the Custom Networking section of the Oracle Java Tutorial.
    – user207421
    Aug 7, 2015 at 1:39
  • Or move to a different strategy using select. Then you can keep using a single thread. Aug 7, 2015 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.