创建延迟,直到收到一定数量的消息

时间:2015-10-22 21:06:06

标签: java sockets delay

我使用套接字协议在服务器程序和某些客户端程序之间发送消息

我希望在服务器程序中添加代码,使其等到收到一定数量的消息(即每个客户端一个消息)之后

我该如何编码?

一些客户代码: -

 String message_string = Integer.toString(message_code);

 // send details of prospective trade to the Server
 try {
     InetAddress host = InetAddress.getLocalHost();
     Socket socket = new Socket(host.getHostName(), 7001);
     ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
     outputStream.writeObject(name + " " + message_string);
     ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
     String message = (String) inputStream.readObject();
     myConsole.getOut().println("Message: " + message);
     inputStream.close();
     outputStream.close();
 } catch (UnknownHostException e) {
      e.printStackTrace();
 } catch (IOException e) {
     e.printStackTrace();
 } catch (ClassNotFoundException e) {
      e.printStackTrace();
 }
// now wait until the list of optimized trades is received back from the Server

我可以看到各种客户'邮件到达服务器。

一旦消息数量达到一定数量(即客户数量),我希望服务器代码继续但如果不是我希望服务器等待

1 个答案:

答案 0 :(得分:0)

要清楚

服务器接收来自每个客户的预期交易

一旦所有预期交易进入,服务器会进行一些计算,然后将优化交易列表返回给所有客户

所以,我实际上有两个等待'需要的时间

一个用于服务器,一个用于每个客户端

相关问题