您的代码片段没有任何问题,我能感觉到的唯一问题是您的客户端代码是如何实现的。假设您的客户端代码是这样的。
Socket clientSocket = new Socket(ip, port);
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
while ( true ){
String sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
outToServer.flush();
DataInputStream is = new DataInputStream(clientSocket.getInputStream());
System.out.println(is.readLine());
}
现在这个客户端正在从控制台读取输入并将其发送到服务器,发布它阻塞等待响应。让我们假设如果他收到消息'fire',服务器将向所有人发送数据[这是你想要的] 。现在有两个客户端连接到服务器,ClientA和ClientB。
1. ClientA blocked at user-input (console)
2. User enter 'abc'
3. ClientA moves on sends 'abc' to server
4. ClientA blocks to read data from server
5. Server sends back 'abc' to ClientA [Assuming its an echo server]
6. ClientA reads the data, print it
7. ClientA moves back(while loop), blocked again for user input.
ClientB 也发生了同样的事情,所以现在两者都被阻止了来自控制台的用户输入。
现在假设 ClientA 用户发送我们的魔法词 ['fire' 在这种情况下],服务器识别它是一个魔法词并开始向 ClientA 和 ClientB 发送数据,
现在这里需要注意的重要一点,即 ClientA 和 ClientB 的状态,此时ClientA刚刚发送数据并在点4,因此被阻止从服务器读取数据,因此当服务器发送消息时它可以读取和显示,而客户端B在点1,被阻止从控制台获取数据..所以即使服务器已经发送数据 ClientB 有数据要读取,但它不能因为它卡在第 1 点,你也在写入后关闭与服务器的连接,所以如果 ClientB 不知何故从点 1 移动到点4,套接字已经关闭,因此它再次无法读取。虽然服务器已经向 ClientA 和 ClientB 发送了数据,但只有 ClientA 能够得到它。
呸!!很长的解释,我想它会给你一些解决问题的方向
服务器代码供参考
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class BroadCastTCPServer {
private static ArrayList<Socket> sockets = new ArrayList<Socket>();
//Usage: java TCPServer port
public static void main(String args[]){
BroadCastTCPServer server = new BroadCastTCPServer();
try {
server.startServer("9999");
} catch (Exception e) {
e.printStackTrace();
}
}
private void startServer(String portStr) throws IOException {
int port = Integer.parseInt(portStr);
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Listening on IP:" + serverSocket.getInetAddress().getHostAddress() + " Port:"+port);
while(true){
Socket connectionSocket = serverSocket.accept();
sockets.add(connectionSocket);
System.out.println("New client connection:" + connectionSocket.getRemoteSocketAddress());
ClientHandler cl = new ClientHandler(connectionSocket);
Thread clientThread = new Thread(cl);
clientThread.start();
}
}
public void sendAll() throws Exception{
System.out.println("No of connections:" + sockets.size());
for(Socket current: sockets){
System.out.println(current.getRemoteSocketAddress());
ObjectOutputStream out=new ObjectOutputStream (current.getOutputStream());
out.flush();
out.writeObject("Tata\n");
out.flush();
//out.close();
}
}
class ClientHandler implements Runnable{
Socket socket;
public ClientHandler(Socket socket){
this.socket = socket;
}
@Override
public void run() {
BufferedReader inFromClient = null;
ObjectOutputStream out = null;
BufferedWriter br = null;
try {
inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new ObjectOutputStream (socket.getOutputStream());
OutputStreamWriter or= new OutputStreamWriter(out);
br = new BufferedWriter(or);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true){
String clientSentence= null;
try {
clientSentence = inFromClient.readLine() ;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Received: " + clientSentence);
if (clientSentence.equalsIgnoreCase("fire")) {
try {
sendAll();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
br.write(clientSentence + "\n" );
br.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}