问题
I have been suffering a huge problem in my server to send an object received from a client to another client. This object is a ChatMessage. My server is able to send that message to the same client, but I can not think of a way to route this message to another client.
Please help Thank you very much in advance.
回答1:
The server needs both client connections open at the same time and a way to know that a message received from client-1 should be routed to client-2. Your protocol needs to handle BOTH client-to-server and server-to-client requests.
回答2:
There's any number of examples on the net for this. A quick google brought up this tutorial:
http://programming-guides.com/java/tcp-client-server-chat
You need to maintain multiple connections, one for each client, and send the data to the appropriate clients when you receive it.
来源:https://stackoverflow.com/questions/5914520/how-to-read-from-a-client-and-send-to-another-client-through-a-server-socket-in