• Post Reply Bookmark Topic Watch Topic
  • New Topic

Handle Multiple clients

 
Rohan Kalbhor
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rohan Kalbhor
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!