所以这里是交易:
我有一个正在运行的服务器,该服务器不断通过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 :通过流发送对象。
  • 09-04 09:41
    查看更多