问题
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>();
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)
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.
What is the best way to do it?
回答1:
Encapsulate the output stream of the socket with the appropriate wrapper:
- DataOutputStream: To send primitive data type + Strings.
- ObjectOutputStream: To send objects through the stream.
来源:https://stackoverflow.com/questions/13576596/recive-different-type-of-objects-on-the-same-tcp-socket