2017-10-05 52 views
0

假设我有以下方法:如何排除或跳过广播消息给多个客户端(多线程Java的插座)当发件人

static synchronized void broadcast(String message, String name) throws IOException 
{ 
     // Sends the message to every client including the sender. 
     Socket s; 
     PrintWriter p; 
     for (int i = 0; i < clientList.size(); i++) 
     { 
      s = clientList.get(i); 
      p = new PrintWriter(s.getOutputStream(), true); 
      p.println(name+": "+message); 
     } 
} 

注意:这行// Sends the message to every client including the sender

目标:更改方法,以便它将://Sends the message to every client EXCEPT the sender

我可以对上面的代码做出什么修改,以便广泛cast方法会将邮件发送给每个客户端排除发件人?

这导致了它被发送到所有客户端上线是for循环与线相结合,说p.println(name+": "+message);

有些人已经开始使用提出了hashmap为:

Map<Integer, java.net.Socket> clients = new HashMap<Integer, java.net.Socket>(); 但如何将我在for循环中使用该散列映射向除发送者之外的每个客户端广播? 有人可以提供一个例子,其中hashmaps用于排除 客户端从消息被广播给他们吗?目前我有一个名为clientList的变量,我应该用hashmap替换它吗?

的代码完全上下文:

import java.io.*; 
import java.net.*; 
import java.util.*; 
public class ChatServer 
{ 

     /* 
     * Sets up a server for multiple conversations. 
     * 
     * Join in by typing 
     * telnet x y 
     * where x and y are the computer's name and port as 
     * given when the Chatter starts. 
     * 
     */ 

     private static LinkedList<Socket> clientList = new LinkedList<Socket>(); 

     public static void main(String[] args) throws IOException 
     { 
      // Get the port and create a socket there. 
      int port = 8190; 
      ServerSocket listener = new ServerSocket(port); 
      System.out.println("The Chat Server is running on port "+port); 

      // Listen for clients. Start a new handler for each. 
      // Add each client to the list. 
      while (true) 
      { 
       Socket client = listener.accept(); 
       new ChatHandler(client).start(); 
       System.out.println("New client on client's port "+ client.getPort()); 
       clientList.add(client); 
      } 
     } 

     static synchronized void broadcast(String message, String name) throws IOException 
     { 
      // Sends the message to every client including the sender. 
      Socket s; 
      PrintWriter p; 
      for (int i = 0; i < clientList.size(); i++) 
      { 
       s = clientList.get(i); 
       p = new PrintWriter(s.getOutputStream(), true); 
       p.println(name+": "+message); 
      } 
     } 
     static synchronized void remove(Socket s) 
     { 
      clientList.remove(s); 
     }  
} 

    class ChatHandler extends Thread 
    { 

     /* The Chat Handler class is called from the Chat Server: 
     * one thread for each client coming in to chat. 
     */ 

     private BufferedReader in; 
     private PrintWriter out; 
     private Socket toClient; 
     private String name; 

     ChatHandler(Socket s) 
     { 
      toClient = s; 
     } 

     public void run() 
     { 
      try 
      { 
       /* Create i-o streams through the socket we were given 
       * when the thread was instantiated and welcome the new client. 
       */ 

       in = new BufferedReader(new InputStreamReader(toClient.getInputStream())); 
       out = new PrintWriter(toClient.getOutputStream(), true); 
       out.println("*** Welcome to the Chatter ***"); 
       out.println("Type BYE to end"); 
       out.print("What is your name? "); 
       out.flush(); 
       String name = in.readLine(); 
       ChatServer.broadcast(name+" has joined the discussion.", "Chatter"); 

       // Read lines and send them off for broadcasting. 
       while (true) 
       { 
        String s = in.readLine(); 
        if (s.startsWith("BYE")) 
        { 
         ChatServer.broadcast(name+" has left the discussion.", "Chatter"); 
         break; 
        } 
        ChatServer.broadcast(s, name); 
       } 
       ChatServer.remove(toClient); 
       toClient.close(); 
      } 
      catch (Exception e) 
      { 
       System.out.println("Chatter error: "+e); 
      } 
     } 
    } 
+0

方法添加UTF-8('StandardCharsets.UTF_8')同时'InputStreamReader'和'PrintWriter'所以希腊语,表情符号等可以在不同的计算机上传播。否则,每台电脑都使用其默认编码。 –

回答

1
static synchronized void broadcast(Socket sender, String message, String name) throws IOException { //Add the sender socket here. 
     // Sends the message to every client including the sender. 
     Socket s; 
     PrintWriter p; 
     for (int i = 0; i < clientList.size(); i++) 
     { 
      s = clientList.get(i); 
      if(s != sender) { //If the client is not equal to sender. 
       p = new PrintWriter(s.getOutputStream(), true); 
       p.println(name+": "+message); 
      } 
     } 
} 

呼叫这样

ChatServer.broadcast(toClient, s, name); //The toClient variable is inside your chathandler which is the reference to the socket which is the sender. 
相关问题