【问题标题】:Making a multithreaded chat server with sockets使用套接字制作多线程聊天服务器
【发布时间】:2013-03-20 08:28:39
【问题描述】:

我正在使用 TCP 套接字对服务器聊天进行编码。我实施了公共和私人信息。现在,我该如何制作频道?如何将通道与套接字客户端链接?我做了一个这样的String[]

if (frase.startsWith("/make")) {
    //crea sala
    String[] privado = frase.split("\\s", 2);
    synchronized (this) {
        end = false;
        for (int i = 0; i < MAX && !end; i++){
            if (salas[i] == null) {
                canal = privado[1];
                salas[i] = canal;
                end = true;
            } else if (privado[1].startsWith(salas[i])) {
                salidaACliente.println("Ya existe " + privado[1] + "\n");
                end = true;
            }
            if (i == MAX - 1) {
                salidaACliente.println("Espacio de canales lleno.\n");
                end = true;
            }
        }
    }
}

例如:

  • 0-channel1
  • 1-channel2

所有用户都可以使用命令/seechannels查看创建的频道

String[] salas = new salas[20];

但到目前为止,频道只是String。我现在如何使用/join channel1 将通道与套接字链接?

【问题讨论】:

    标签: java tcp chat channels


    【解决方案1】:

    例如,您可以在所有线程中创建一个数组,用于存储用户所属的频道。

    boolean joinedChannels[] = new Boolean[max_channels];
    
    // Remember to intialize the array.
    
    if (cmd == "/joinchannel") {            // cmd here is the issued command
       joinedChannels[args[0]] = true;      // args[] is an array of the arguments
                                            // following the command. This sets the
                                            // desired channel to be active.
    } else if (cmd == "/leavechannel") {
       joinedChannels[args[0]] = false;     // And this here sets it inactive.
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 2016-03-18
      • 2020-10-10
      • 2013-04-09
      • 2015-02-17
      相关资源
      最近更新 更多