假設我有以下方法:如何排除或跳過廣播消息給多個客戶端(多線程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);
}
}
}
方法添加UTF-8('StandardCharsets.UTF_8')同時'InputStreamReader'和'PrintWriter'所以希臘語,表情符號等可以在不同的計算機上傳播。否則,每臺電腦都使用其默認編碼。 –