发送数值客户端 - 服务器(套接字)

时间:2021-01-25 21:45:17

标签: java

我在将数值从我的服务器发送到客户端时遇到问题。我保存在 ArrayList 中的客户端套接字,我可以连接多个客户端。我的 for 发送随机数值,但直到我完成了 socketCliente1,例如继续下面的 socketCliente2,如果你能帮助我,我将非常感激......

    public void generarNumeroRandom() {
      for (int i = 0; i < arregloNumeroAleatorio.length; i++) {
      int numeroAleatorio = (int) (Math.random() * 25 + 1);
      arregloNumeroAleatorio[i] = numeroAleatorio;
      for (int j = 0; j < i; j++) {
         if (arregloNumeroAleatorio[i] == arregloNumeroAleatorio[j]) {
              i--;
         }
      }
   }
   }

public void enviarNumeroAleatorioCliente() throws InterruptedException {

generarNumeroRandom();        
for (Socket listaJugadoresConectados : listaJugadores) {
    try {
        DataOutputStream salidaDos = new DataOutputStream(listaJugadoresConectados.getOutputStream());
        for (int i = 0; i < arregloNumeroAleatorio.length; i++) {
            Thread.sleep(5000);
            salidaDos.writeUTF(String.valueOf(arregloNumeroAleatorio[i]));
        }
    } catch (IOException ex) {
        Logger.getLogger(HiloServidor.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

0 个答案:

没有答案