This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Handle Multiple clients Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Handle Multiple clients" Watch "Handle Multiple clients" New topic
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Handle Multiple clients
 
Similar Threads
servlet-applet
Synchronizing the clocks
Design for Chat Server
Sending data from client to Server
Client Server timer Synchronization