How to Read from a client and send to another client through a server socket in Java

 ̄綄美尐妖づ 提交于 2019-12-25 00:08:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!