繁体   English   中英

使用套接字制作多线程聊天服务器

[英]Making a multithreaded chat server with sockets

我正在使用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通道1
  • 1通道2

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

String[] salas = new salas[20];

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

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

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.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM