This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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.
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.
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