0

How can I stop a server socket which is listening client. My code look like below

 private ServerSocket serverSocket;

class ServerThread implements Runnable {

    private BufferedReader input;
    public void run() {
    Socket socket = null;

        try {
            serverSocket = new ServerSocket(SERVE_LISTEN_RPORT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        while (!Thread.currentThread().isInterrupted()) {

            try {

                socket = serverSocket.accept();
                try {

        this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                     String read = input.readLine();
                                 //Do something
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }


             try {
              serverSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }
}

I need to stop this thread when I got a command from another thread. I already sow this question but no idea how to implement it.

Community
  • 1
  • 1
Haris
  • 13,645
  • 12
  • 90
  • 121

2 Answers2

2

Here's an example how to close a socket from other thread:

private static volatile ServerSocket serverSocket;

public static void main(String[] args) throws InterruptedException, IOException {
    Thread serverThread = new Thread(new ServerThread());
    serverThread.start();
    Thread.sleep(1000); // wait a  bit, then close
    serverSocket.close();
}

static class ServerThread implements Runnable {

    private BufferedReader input;

    public void run() {
        try {
            serverSocket = new ServerSocket(25);
            while (true) {
                Socket socket = serverSocket.accept();
                // client request handling logic
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
Jk1
  • 11,233
  • 9
  • 54
  • 64
  • But I am getting an exception when closing serverSocket after accepting connection, I am doing this on android – Haris Aug 31 '13 at 10:22
  • 2
    Your code is neither synchronized and nor is your `ServerSocket` `volatile` - this is going to cause some horribly unpredictable race hazards. – Boris the Spider Aug 31 '13 at 10:25
  • @Haris, accept(...) by contract should either return a socket or throw an exception. In our case there's obviously no socket to return, so exception is thrown. My advice here is to simply ignore it. – Jk1 Aug 31 '13 at 10:28
1

The server socket should be outside the run() method and should be shared by the threads.

Now when you need to interrupt the Thread which is calling socket.accept(), the other thread can call socket.close() and accept() will throw exception.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120