• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multi-Thread server, accept many clients but handles one at a time?

 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I am busy writing a client server pair. There must be the ability for many users to connect to the server at the same time.

Now I have written the pair using a Thread to handle the accepted socket, but I find that while many clients can connect, that the server is interacting only with one client at a time. Is this the way that it is supposed to behave? How would I get my server to interact with up to x clients at the same time?

Server - the part where the connection is accepted


SecureThread - the constructor part of the thread.



If there is any other code that you would need to help me solve this, please just let me know.

Kind regards,
Rachel
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread has a snippet of code I use for this. It looks very similar to yours and seems to work. Maybe I should prove to myself that it's handling multiple threads at the same time correctly!
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I hate to say it, but I tried with the setSoTimeout and it didn't make a difference. It just meant that I had to be faster when getting the clients to run because the time I had to connect to the "server" was limited.

But, there must be people who've accomplished this correctly, which means that there has to be a way to do it - so I'm just going to slog it out until I crack it.

Do you by any chance have any other suggestions that might get me to where I need to be?

Thank you so much for your time!
Rachel
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachael,

How are you testing your concurrency? I mean are you sure that the most recent thread isn't just being prioritized?
[ August 17, 2004: Message edited by: Rovas Kram ]
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Rovas said. The code you've shown us so far, the server and the constructor for SecureThread, all run in the same thread. Does SecureThread.run do anything?
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't think to check that, I'll get on it right away!

In pseudo code, what the thread does, is accept the socket and then communicates back with the client passing messages and files and listening back from messages and files from the client. (It's not a chat server, just an automated receiving and sending files.) It's a lot of code to post, but I will if you want.

So the thread that you see there is the only thread that I've asked to run.

I know that there is a solution with passing the socket to another temporary port but it looked kind of choppy so I was wondering why this solution I have now wasn't working.

Cheers,
Rachel
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went and had a look at the priotiries and the connections to the server.

Originally, while I was still seeing how it all fitted together, I could get many clients to connect and the server handled them all as they came in (just like I want it). For this pair the following was happening...
Client: Hello server
Server: Hello client
Client (waits for user input)
Client: User said ...
Server: Thanks. (if user says Bye then disconnect.)

So this all worked fine. But now that I have no pause (waiting for users input) I see now that the other clients aren't getting accepted until the first one is finished? Why is that?

I'll bet it's something to do with that thread, so I'll keep with that in the meantime...

Cheers,
Rachel
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rachel Swailes:

So this all worked fine. But now that I have no pause (waiting for users input) I see now that the other clients aren't getting accepted until the first one is finished? Why is that?


Again, without the code I'm only guessing, but by waiting for input from the user the worker thread is surrendering the CPU to work on other threads. Threads of equal priority won't interrupt each other in the Java VM, so once your worker thread starts he either runs until completion or until the VM switches to another thread (on platforms that support time-slicing). You can try setting the priority of your worker thread lower than the main thread or by placing a Thread.yield() call or two in strategic points of the code.
Just to make sure we're on the same page, SecureThread extends Thread and the work in SecureThread is done in run() and not in start(), right?
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe

Yes, SecureThread implements Runnable (and it's run from the server) and the work is done in run()! Yes, I made sure when the problems started that I hadn't done something like that! ;-) (But thanks for checking)

So I now went and put in code to listen for user inputs at certain places in the client and then the server will take on other clients. So I'm sure that you are right about the threads blocking each other out. I took your advice and tried the Thread.yield() in two places but it didn't behave either.

So here is the code for all three classes, SecureClient, SecureServer and SecureThread

[CODE TAKEN OUT BY ME. It served the purpose of having someone be able to help and I don't want everyone to have access to all my hard work!]

Thank you so much for taking the time to help!

Cheers,
Rachel

[ August 18, 2004: Message edited by: Rachel Swailes ]
[ August 19, 2004: Message edited by: Rachel Swailes ]
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also put in a Thread.sleep() after the SecureThread validates the client's connection. It does the trick and then as many clients as I can activate all get dealt with like I wanted. But it's a bit of a work-around to a good solution. So hopefully someone can suggest something more suitable.

Thanks again
Rachel
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at your code I notice that you have the file which is uploaded from the client hardcoded in the client code. Don't you think there would be some sort of file lock that would prevent multiple server threads from writing to the same file at the same time? That could be contributing to the single-threaded behavior.
What platform are you using? Java doesn't gaurantee time-slicing, so some tinkering with priorities may be in order.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks to be the answer! I made the server save to different files and I made the clients each send different files and it's working just like I need it to.

At least when running into problems with code we get a change to learn a lot more than if it were straight forward every time!

Thank you so much for your help, Joe. You rock man!!

Take care,
Rachel
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!