如何通过 Java 中的服务器套接字从客户端读取数据并将其发送到另一个客户端
我的服务器在将从客户端接收到的对象发送到另一个客户端时遇到了巨大的问题。该对象是一个 ChatMessage。 我的服务器能够将该消息发送到同一个客户端,但我想不出一种方法将该消息路由到另一个客户端。
请帮忙 预先非常感谢您。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
服务器需要同时打开两个客户端连接,并且需要一种方法来知道从客户端 1 接收到的消息应路由到客户端 2。您的协议需要处理客户端到服务器和服务器到客户端的请求。
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.
网上有很多这方面的例子。快速谷歌显示了本教程:
http://programming-guides.com/java /tcp-client-server-chat
您需要维护多个连接,每个客户端一个连接,并在收到数据时将数据发送到适当的客户端。
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.