0

I couldn't find a similar post here, so here it is:

When I start the server and a couple of clients, I want the users on all clients to be able to input data to be processed. Also, I want the server to be able to exit the listening loop once all client sessions are terminated.

Server code listening loop:

//Console console = System.console();
// or 
//InputStreamReader str = new InputStreamReader(System.in);
//BufferedReader uinp = new BufferedReader(str);

// ...

try (ServerSocket serverSocket = new ServerSocket(portNumber)) { 
    while (listening) {
        new SocketServerThread(serverSocket.accept()).start();

        //String serverInput = console.readLine("> ");
        //if ( serverInput.equalsIgnoreCase("exit") ) { listening = false; }
        // or
        //System.out.print("> ");
        //if ( (uinp.readLine()).equalsIgnoreCase("exit") ) { listening = false; }
    }
} catch (IOException e) {
    System.err.println("Could not listen on port " + portNumber);
    System.exit(-1);
}

Here's the thing: 1) If I code the server to be able to receive input and process it (like an "exit" string to tell the server to exit the listening loop), it seems to me that the second client's thread won't run, which I find quite peculiar. 2) If I don't code the server to be able to exit the listening loop, how can the server exit the loop at the user's will?

As you can see, I've tried (a) Console and (b) InputStreamReader and BufferedReader, but none of them worked.

So, my question is this: how can I have the server quit the listening loop at the user's will and allow the second client's thread run at the same time?

9
  • I can't see any BufferedReader etc here. What's the question? Commented Nov 17, 2013 at 11:41
  • @EJP : So, my question is this: how can I have the server quit the listening loop at the user's will and allow the second client's thread run at the same time? Commented Nov 17, 2013 at 11:51
  • 1
    @Andrey I don't understand the question. Obviously, if the server shuts down, the clients won't be able to talk to it, because it won't exist. That's just common sense. Or did you mean something else? Commented Nov 17, 2013 at 14:54
  • 1
    sigh I still don't understand. When is it appropriate to stop the server, according to you? Commented Nov 17, 2013 at 17:28
  • 1
    Yes, so what's the problem? Commented Nov 17, 2013 at 18:10

1 Answer 1

2

I think you have confused the purpose of client server architecture.
A server is a centralized system to which clients can connect and request one or more services. So what you're saying is that you want to stop the centralized system i-e the server and still let other clients keep running? This is not possible. Because a client can not exist without a server (It won't be called a client if it doesn't has a server in the first place)

So if you quit server's listening loop, no client can connect to it. Hence no client thread can be executed. If you want to that all server-client sessions are terminated at the will of the clients' users. U can do a workaround.

Keep a count variable at the server end. Increment it with each new client coming and decrement on the client leaving. Also, keep a boolean variable like toClose initialised with false at server end again. Now let's say client 'A' tells server to close (like "exit" command as u have already done), you change the value of toClose to true.
Now at the end of each client's session, you check something like this:

if (toClose == true && count <=0 )
    exitListeningLoop()
Sign up to request clarification or add additional context in comments.

5 Comments

I know that. As I've said in my post, I want the server to be able to quit listening once all server-client sessions are terminated at the will of the clients' users. I can't have the server run forever, right?
OK. What tools do I need to use to let the server see whether a client has joined or left in order to keep count of the clients present?
Well, u can increment this variable in the start of your run() method of ServerSocketThread. Because whenever this method is executed, this means that a new client has arrived. Also, u can decrement the counter at the end of this thread.
I guess what you are saying is that I have to initialize a counter and pass it to the thread along with the socket, right?
That's one way. OR (if your SocketServerThread is an inner class of your Server) you may declare a static member variable of Server class and then access it from run method.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.