[英]Recive different type of objects on the same TCP socket
所以这是交易:我有一个正在运行的服务器,该服务器不断通过TCP套接字接受客户端:
public ArrayList<Socket> lista_users = new ArrayList<Socket>();
套接字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)
将套接字保存在阵列列表中。 之后,我想向我的客户发送不同类型的信息(线程“ trata_cliente”)。 当我说不同类型的信息时,请先发送警告消息,然后发送对象,然后发送一些消息,然后再次发送对象。
最好的方法是什么?
用适当的包装器封装套接字的输出流:
DataOutputStream
:发送原始数据类型+字符串。 ObjectOutputStream
:通过流发送对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.