Recive different type of objects on the same TCP socket

只谈情不闲聊 提交于 2019-12-11 01:55:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!