2013-03-29 92 views
0

我使用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将通道与套接字链接?

回答

0

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

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. 
} 
相关问题