File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes communicating multiple sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "communicating multiple sockets" Watch "communicating multiple sockets" New topic

communicating multiple sockets

Brandon Broschinsky
Ranch Hand

Joined: Aug 21, 2004
Posts: 41
I have a program written with a threaded server. The server can accept connections from multiple clients. The clients can transmit a string to the server, which the server outputs and echoes back to the client that sent it. I am trying to get the server to transmit any string recieved from any client to all clients. i.e. if a client sends a message to the server it echoes it back to all the clients that are connected to it. I am totally lost with this. Any help would be appreciated.
Alex Pakhomov

Joined: Mar 12, 2001
Posts: 11
Is multi-threading a requirement? Looks like you'd be much better off with a "queued" server. Otherwise you'll have a lot of issues with thread synchronization. Normally threads in a threaded server don't interact.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
One approach is for the client and server to go both ways with client and server roles. This sounds confusing but it's not so bad.

It's hard to show the logic for processes on different threads and JVMs this way ... did it make any sense?

Oh, I didn't show any new threads. If the server broadcasts to all clients because one client has sent a new message, that for loop would run on the thread that got the client connection. It might take a while but you'd be able to return the number of successful or failed sends to the original client. Depending on the server environment, you might spin off "n" threads each broadcasting to 1/n clients. My current application will have this problem Real Soon Now and I expect to do exactly that in the servlet container.
[ October 28, 2004: 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
I agree. Here's the link:
subject: communicating multiple sockets
It's not a secret anymore!