简体   繁体   English

向多个客户端广播对象

[英]Broadcasting Objects to multiple clients

Currently I try to implement a Server/Client - Application and came up with a problem, I cant find a solution for. 目前,我尝试实现服务器/客户端-应用程序,并提出了一个问题,但找不到解决方案。

Im running a Server , which is waiting for Clients to login, adding them to a cachedThreadPool and starting a new Runnable for each Client , to handle their individual requests etc. 我正在运行一个Server ,等待Clients登录,将它们添加到cachedThreadPool并为每个Client启动一个新的Runnable ,以处理其各自的请求等。

Now there are some answers to these requests, which have to be broadcasted after the server did his processing. 现在,对这些请求有一些答案,必须在服务器进行处理后广播这些答案。

        this.mOos = new ObjectOutputStream(this.mSocket.getOutputStream());
        this.mOos.flush();
        this.mOis = new ObjectInputStream(this.mSocket.getInputStream());

        while(true)
        {
            Object o = this.mOis.readObject();

            if(o !=null && this.mInputList.size() < 20)
            {
                this.mInputList.add(o);
            }

            if(!this.mInputList.isEmpty())
            {
                handleIncomingObject();
            }

            if(!this.mOutputList.isEmpty())
            {                       
                try 
                {
                    this.mOos.writeObject(this.mOutputList.remove(0));
                    this.mOos.flush();
                    this.mOos.reset();
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }               
        }

Now what I'm having trouble with is, that when I process a request in: 现在,我遇到的麻烦是,当我在其中处理请求时:

handleIncomingObject();

which needs to be broadcasted. 需要广播。

My approach is to give a reference to the ExecutorService Object, when I create the runnable, to have access to the threadpool IN the different threads. 我的方法是在创建可运行对象时提供对ExecutorService对象的引用,以访问不同线程中的线程池。

As you may see, every client got his own 如您所见,每个客户都有自己的

LinkedList<Object> mInputList
LinkedList<Object> mOutputList

to buffer incoming and outgoing messages. 缓冲传入和传出的消息。

In handleIncomingObject() I'd like to do something like: handleIncomingObject()我想执行以下操作:

//assume exec as ExecutorService
for(Thread t : exec)
{
        mOutputList.add(new Message(message));
}

Thanks in advance any every help or suggestions. 在此先感谢您的任何帮助或建议。

Greetings, Ben. 问候,本。

Use a Vector containing all clients and iterate over this Vector to send broadcast messages. 使用包含所有客户端的Vector,并在此Vector上进行迭代以发送广播消息。 You can use a method like: 您可以使用如下方法:

public static synchronized void broadcast(Message msg) {
    for(Client c : vector) {
        c.mOutputList.add(msg);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java中的TCP连接广播到多个客户端 - Broadcasting to multiple clients using TCP connection in Java 广播到多个客户端的MulticastSocket与DatagramSocket - MulticastSocket vs. DatagramSocket in Broadcasting to Multiple Clients 通过套接字java将对象广播到多个客户端 - Broadcasting object to multiple clients through socket java 向多个客户端广播消息时如何排除或跳过发件人(多线程Java套接字) - How to exclude or skip the sender when broadcasting a message to multiple clients (multithreaded java socket) Google App Eninge Channel API可用于向多个客户端广播JSON数据吗? - Google App Eninge Channel API usable for broadcasting JSON-Data to multiple clients? 从多个客户端更新服务器上对象的模式/最佳实践 - Pattern/Best practice for updating objects on server from multiple clients 多客户聊天程序,向所有客户广播聊天? - Multi-client chat program, broadcasting chat to all clients? 单个程序如何与多个客户端共享? (意味着多个客户端线程共享相同的对象吗?) - How a single program is shared with multiple clients? (Means do multiple client threads share same objects?) 如何在多个Axis2 Web服务客户端之间共享代理对象? - how can I share proxy objects across multiple Axis2 web service clients? Java中多个客户端的超时 - Timeouts for multiple clients in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM