如何通过Java中的服务器套接字从客户端读取并发送到另一个客户端 [英] How to Read from a client and send to another client through a server socket in 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.
推荐答案
网上有许多示例.一个快速的Google提出了本教程:
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-服务器聊天
您需要维护多个连接,每个客户端一个,然后在收到数据后将数据发送到相应的客户端.
You need to maintain multiple connections, one for each client, and send the data to the appropriate clients when you receive it.
这篇关于如何通过Java中的服务器套接字从客户端读取并发送到另一个客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!