| Author |
Handle Multiple clients
|
Rohan Kalbhor
Ranch Hand
Joined: Aug 18, 2006
Posts: 78
|
|
Hello all, i am devicing a software which uses a client-server architecture for file transfer using sockets the flow of file from client1 to client2 is client1->Server->client2 my problem is to handle multiple clients at a time since the will be many clients using the software simultaneously, there will be continous transfer of files on the server How do i handle more than one client at a time at the server side i guess i should go in for multithreading but i am getting how exact am i gonna write the code can anyone please help me in handling the clients Thanks in Advance Rohan Kalbhor
|
..............................<br />Exceptions are a part of possibility<br />Errors are a part of truth<br />................................
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Yes, threads are in your future. Socket servers often look like this: The one line in the try block does several things. "serverSocket.accept()" gives you a new socket to communicate with the client. "new MessageHandler()" creates a new object to work on that socket. MessageHandler is my own class; yours may be very different, but it has to implement Runnable. And "executor.execute()" tells the thread pool to queue up the new MessageHandler to run on another thread. Does that answer the right question? [ March 20, 2007: Message edited by: Stan James ]
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Rohan Kalbhor
Ranch Hand
Joined: Aug 18, 2006
Posts: 78
|
|
Thankyou Mr.James, i think thats the solution i was waitinf for i will try it out and revert back to you as soon as possible,
|
 |
Rohan Kalbhor
Ranch Hand
Joined: Aug 18, 2006
Posts: 78
|
|
Thankyou Mr.James, i think thats the solution i was waiting for i will try it out and revert back to you as soon as possible,
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
I'm glad that connected with what you need. As you dig into it, read up on Executors and the thread pools it can create. The one in the example is set up to make any number of threads so a huge number of concurrent users could overrun the server. You might want to constrain it to a fixed number which can help avoid being buried but may make requests wait until a thread becomes available.
|
 |
 |
|
|
subject: Handle Multiple clients
|
|
|