*
The moose likes Threads and Synchronization and the fly likes Multiple clients' input served by only one server thread. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multiple clients Watch "Multiple clients New topic
Forums: Sockets and Internet Protocols Threads and Synchronization
Author

Multiple clients' input served by only one server thread.

Nikolaos Plastiras
Greenhorn

Joined: Feb 08, 2012
Posts: 7

I am trying to find a way to read the input (a simple string) of multiple clients using one thread (besides main thread) only on the server side.

Thought something like when clientSocket = serverSocket.accept() happens, the socket would be added into a collection and then a single thread (instead of starting a new thread per client to listen) could read the sockets' collection and then read the input of each socket in an endless while loop.

I have problem cycling through the collection since main thread adds an element in the collection while another thread tries to cycle throught. Can't "see" second client while iterating.

Have already tried ArrayList and CopyOnWriteArrayList (java.util.concurrent.*)

Any suggestions?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't really see why you have a problem. If you're processing the list and you don't see a new entry because it was just added, then you're going to see it the next time you process the list.

I'm not aware of any standard Java collections which will do what I think you're asking. I think that's because, as I said, it isn't really a problem. Unless I'm not understanding your setup, which is definitely a possibility. Your reference to an "infinite loop" was disturbing, and I couldn't see what "and then" really meant.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I don't really understand what problem you're having either. If you're properly using synchronization or appropriate classes from java.util.concurrent, you won't have problems with inconsistent state in the list.

One potential problem I do see (though maybe not, depending on what you know about your clients' behavior), is starvation. If you're looping through the list calling a blocking read against one client, then the next, then the next, and so on, it could hang indefinitely if the current client doesn't happen to have anything to write, starving out the other clients.
Nikolaos Plastiras
Greenhorn

Joined: Feb 08, 2012
Posts: 7

Below is some code.
handleConnection is called when a new serverSocket.accept() gets a request.
run is the code the thread that handles input from clients runs.
Because of readLine() (I think) thread waits and doesn't read other client's input.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18526
    
  40

Nikolaos Plastiras wrote:Below is some code.
handleConnection is called when a new serverSocket.accept() gets a request.
run is the code the thread that handles input from clients runs.
Because of readLine() (I think) thread waits and doesn't read other client's input.


which is *exactly* what Jeff pointed out ...

Jeff Verdegan wrote:
One potential problem I do see (though maybe not, depending on what you know about your clients' behavior), is starvation. If you're looping through the list calling a blocking read against one client, then the next, then the next, and so on, it could hang indefinitely if the current client doesn't happen to have anything to write, starving out the other clients.


If you need to handle multiple sockets, with one thread, and you don't want one socket blocking the others, you will need to use the NIO library.

Henry

PS.... I am going to add this topic to the networking forum, as it is more network related than threads related.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Multiple clients' input served by only one server thread.
 
Similar Threads
networking and threading!
Bad clients bringing the server down, help.
networking and threading!
Help with thread synchronization
Trying to create a client/server chat