【发布时间】:2016-05-25 09:57:28
【问题描述】:
我想从服务器向所有客户端发送消息。
我的方法是创建一个当前连接到服务器的所有客户端的 ArrayList。 如果任何客户端发送消息,我会遍历 ArrayList 并向每个客户端发送消息。
问题是我的客户没有收到任何消息。
这是我在服务器端的发送方法:
private void message() {
while (true) {
DataInputStream fromClient;
try {
fromClient = new DataInputStream(socketNew.getInputStream());
message = fromClient.readUTF();
for (Socket s:socs) {
System.out.println(message);
DataOutputStream toClient = newDataOutputStream( s.getOutputStream() );
toClient.writeUTF(message);
}
message="";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我在客户端的接收方法:
private void receive_data() {
{
DataInputStream fromServer;
try {
fromServer = new DataInputStream(socket.getInputStream());
message = fromServer.readUTF();
console(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
关于我可能在哪里做错的任何建议?
感谢您的宝贵时间。
【问题讨论】:
标签: java networking client-server chat