0

I'm creating a multithread server that will receive message from client, process and send back the result.

The problem is when I want the server thread to stop, it won't.

Even setting the stopFlag=true, the client is still can connect to server and get the result.

How can I stop the server thread(and may activate it later)?

I tried all of these below but they didn't work.

Close listening ServerSocket

Java server socket program after one day it stops listening

Close listening ServerSocket http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html

public class ServerStart implements Runnable{

private JTextArea servLog;
private boolean stopFlag=false;
public ServerStart(JTextArea servLog){
    this.servLog=servLog;
}

public void setStopFlag(boolean stop){

    stopFlag=stop;

}


@Override
public void run() {
    int port=444;
    try {
        ServerSocket serverSock = new ServerSocket(port);

        synchronized(this){
            while(!stopFlag){
                Socket clientSock=null;
                try{
                clientSock = serverSock.accept();



                }catch(IOException e){
                    if(stopFlag){
                    System.out.println("Server stop");    
                    return;
                    }
                    throw new RuntimeException("Error accepting client connection", e);

                }
                Thread listener = new Thread(new Socket_test(clientSock, port,servLog));
                listener.start();
            }

                serverSock.close();
        }




    } catch (IOException ex) {
        Logger.getLogger(ServerStart.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

Community
  • 1
  • 1
raynus
  • 195
  • 2
  • 8
  • It's good that you mention what you have already tried, but please also add what exactly didn't work when you tried each of those solutions. – RealSkeptic Nov 01 '15 at 16:03
  • Well, they don't show any error. The client and server run fine. The only problem is that somehow I can't close the server thread even I set the stopFlag. – raynus Nov 01 '15 at 16:06
  • Did you really try the third solution *exactly*? Including the fact that there are *synchronized methods* in it? – RealSkeptic Nov 01 '15 at 16:09
  • YES, The third solution put setSoTimeout and one comment suggest to close the server socket. But it didn't work, so I removed them and tried something else. This code is the lastest one I have. – raynus Nov 01 '15 at 16:12
  • No, the third solution used a synchronized method to set the stopping variable. Therefore the stopping variable's value would be visible to the server thread. That's the main point in that solution. Though simply using `volatile` would probably work as well. – RealSkeptic Nov 01 '15 at 16:17
  • Setting the stopflag to volatile static works for me, Thank you!!! – raynus Nov 01 '15 at 16:37

1 Answers1

0

You need to interrupt your socket accept() method which is waiting on something to read from the client. A timeout on the accept could do, or calling close() from another thread

Secondly, your stopRun variable should be volatile so it can be seen on other threads when updated.

Community
  • 1
  • 1
kervin
  • 11,672
  • 5
  • 42
  • 59