Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

communicating multiple sockets

 
Brandon Broschinsky
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic