简体   繁体   English

在同一TCP套接字上接收不同类型的对象

[英]Recive different type of objects on the same TCP socket

so here is the deal: I have a server running that is constantly accepting clients by TCP socket: 所以这是交易:我有一个正在运行的服务器,该服务器不断通过TCP套接字接受客户端:

public ArrayList<Socket> lista_users = new ArrayList<Socket>();

Socket s; 套接字s;

            s = serverSocket.accept();

            lista_users.add(s);
            avisa_all(lista_users, s);

            Thread t_trata_cliente = new Thread(new trata_cliente(lista_users, s));
            t_trata_cliente.start(); //this Thread is responsable for interacting with
                                    //the clients (where my question is)

Saving the sockets on array list. 将套接字保存在阵列列表中。 After i want to send different type of information to my clients(Thread "trata_cliente"). 之后,我想向我的客户发送不同类型的信息(线程“ trata_cliente”)。 When i say different type of information i mean, first send a warning message, then an object, then some message, then object again. 当我说不同类型的信息时,请先发送警告消息,然后发送对象,然后发送一些消息,然后再次发送对象。

What is the best way to do it? 最好的方法是什么?

Encapsulate the output stream of the socket with the appropriate wrapper: 用适当的包装器封装套接字的输出流:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM