[英]java one to one message broadcast socket
I am trying to implement this socket program which broadcast message to all connected clients. 我正在尝试实现此套接字程序,该程序将消息广播到所有连接的客户端。 What I am trying to do is broadcast message to a particular client. 我想做的是向特定客户端广播消息。 I tried to modify 我试图修改
/** Broadcasts the given text to all clients. / **将给定的文本广播给所有客户端。 */ section. */ 部分。
But, couldn't get rid of it. 但是,不能摆脱它。 Can anyone suggest best solution for this. 谁能为此建议最好的解决方案。
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
private static final String USAGE = "Usage: java ChatServer";
/** Default port number on which this server to be run. */
private static final int PORT_NUMBER = 8008;
/** List of print writers associated with current clients,
* one for each. */
private List<PrintWriter> clients;
/** Creates a new server. */
public ChatServer() {
clients = new LinkedList<PrintWriter>();
}
/** Starts the server. */
public void start() {
System.out.println("AndyChat server started on port "
+ PORT_NUMBER + "!");
try {
ServerSocket s = new ServerSocket(PORT_NUMBER);
for (;;) {
Socket incoming = s.accept();
new ClientHandler(incoming).start();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("AndyChat server stopped.");
}
/** Adds a new client identified by the given print writer. */
private void addClient(PrintWriter out) {
synchronized(clients) {
clients.add(out);
}
}
/** Adds the client with given print writer. */
private void removeClient(PrintWriter out) {
synchronized(clients) {
clients.remove(out);
}
}
/** Broadcasts the given text to all clients. */
private void broadcast(String msg) {
for (PrintWriter out: clients) {
out.println(msg);
out.flush();
}
}
public static void main(String[] args) {
if (args.length > 0) {
System.out.println(USAGE);
System.exit(-1);
}
new ChatServer().start();
}
/** A thread to serve a client. This class receive messages from a
* client and broadcasts them to all clients including the message
* sender. */
private class ClientHandler extends Thread {
/** Socket to read client messages. */
private Socket incoming;
/** Creates a hander to serve the client on the given socket. */
public ClientHandler(Socket incoming) {
this.incoming = incoming;
}
/** Starts receiving and broadcasting messages. */
public void run() {
PrintWriter out = null;
try {
out = new PrintWriter(
new OutputStreamWriter(incoming.getOutputStream()));
// inform the server of this new client
ChatServer.this.addClient(out);
out.print("G G Connect! ");
out.flush();
BufferedReader in
= new BufferedReader(
new InputStreamReader(incoming.getInputStream()));
for (;;) {
String msg = in.readLine();
if (msg == null) {
break;
} else {
if (msg.trim().equals("BYE"))
break;
System.out.println("Received: " + msg);
// broadcast the receive message
ChatServer.this.broadcast(msg);
}
}
incoming.close();
ChatServer.this.removeClient(out);
} catch (Exception e) {
if (out != null) {
ChatServer.this.removeClient(out);
}
e.printStackTrace();
}
}
}
}
Broadcasting means sending it to everyone. 广播意味着将其发送给所有人。 When you say "broadcast to one person", do you mean you just want to send a message to a single client ? 当您说“广播给一个人”时,是否意味着您只想向单个客户发送消息?
If that's the case, you need to retrieve the PrintWriter
for that client, and send the message through that object : 如果是这种情况,则需要检索该客户端的PrintWriter
,然后通过该对象发送消息:
private void sendMessage(PrintWriter client, String msg) {
client.println(msg);
client.flush();
}
By the way, instead of using : 顺便说一句,而不是使用:
for (;;) {
}
You should use : 您应该使用:
while (true) {
}
It's easier to recognize as an infinite loop. 更容易将其识别为无限循环。
Oh, and no need to write the ChatServer.this
in ChatServer.this.broadcast(msg);
哦,不需要在ChatServer.this.broadcast(msg);
编写ChatServer.this
ChatServer.this.broadcast(msg);
. 。 You can call the broadcast message just by writing broadcast(msg):
. 您可以通过编写broadcast(msg):
来调用广播消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.