在同一个 TCP 套接字上接收不同类型的对象 [英] Recive different type of objects on the same TCP socket
本文介绍了在同一个 TCP 套接字上接收不同类型的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以这是交易:我有一个正在运行的服务器通过 TCP 套接字不断接受客户端:
so here is the deal: I have a server running that is constantly accepting clients by TCP socket:
public ArrayList<Socket> lista_users = new ArrayList<Socket>();
套接字;
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)
将套接字保存在数组列表中.在我想向我的客户发送不同类型的信息之后(线程trata_cliente").当我说不同类型的信息时,我的意思是,首先发送一条警告消息,然后发送一个对象,然后是一些消息,然后再次发送对象.
Saving the sockets on array list. After i want to send different type of information to my clients(Thread "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.
最好的方法是什么?
推荐答案
用合适的包装器封装套接字的输出流:
Encapsulate the output stream of the socket with the appropriate wrapper:
DataOutputStream
:发送原始数据类型+字符串.ObjectOutputStream
:通过流发送对象.
这篇关于在同一个 TCP 套接字上接收不同类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文