gpt4 book ai didi

Java:将数据从服务器发送到监听套接字的所有客户端

转载 作者:行者123 更新时间:2023-12-02 03:22:23 26 4
gpt4 key购买 nike

我在 java swing 中创建了一个 quizServer 应用程序,它通过套接字连接到多个客户端。我能够通过套接字连接同时从每个客户端向服务器发送数据,但是当我尝试从服务器向客户端发送数据时,只有 1 个客户端接收到该数据。如何修改代码以将数据同时发送到监听套接字的所有客户端?任何代码/伪代码将不胜感激。

这是我的 NetworkClient 类:

public class NetworkClient {

PrintWriter os = null;
Socket s1 = null;
String line = null;
BufferedReader br = null;
BufferedReader is = null;
InetAddress address = null;

void initClient() {
try {
address = InetAddress.getLocalHost();
System.out.println(address);
} catch (UnknownHostException ex) {
Logger.getLogger(NetworkClient.class.getName()).log(Level.SEVERE, null, ex);
}

try {
s1 = new Socket(address, 8888); // You can use static final constant PORT_NUM
br = new BufferedReader(new InputStreamReader(System.in));
is = new BufferedReader(new InputStreamReader(s1.getInputStream()));
os = new PrintWriter(s1.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
System.err.print("IO Exception");

}
}

void sendVal(int data) {
os.println(data);
os.flush();
}

void close() {
try {
is.close();
os.close();
br.close();
s1.close();
} catch (Exception ex) {
Logger.getLogger(NetworkClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

这是我的服务器类:

    public class QuizServer {

QuizJFrame frame;
ServerThread st;

void initServer(QuizJFrame frm) {

frame = frm;
Socket s = null;
ServerSocket ss2 = null;
System.out.println("Server Listening......");
try {
ss2 = new ServerSocket(8888); // port number used as 8888

} catch (IOException e) {
e.printStackTrace();
System.out.println("Server error");
}

while (true) {
try {
s = ss2.accept();
System.out.println("connection Established");
st = new ServerThread(s, frm);
st.start();

} catch (Exception e) {
e.printStackTrace();
System.out.println("Connection Error");

}
}
}
}

这是服务器线程类:

    class ServerThread extends Thread {

BufferedReader is = null;
PrintWriter os = null;
Socket s = null;
QuizJFrame frame;
String question[] = {"", "QUESTION 1", "QUESTION 2", "QUESTION 3", "QUESTION 4", "END"};
int answer[] = {0, 1, 2, 3, 4};
int index;

public ServerThread(Socket s, QuizJFrame frm) {
this.s = s;
frame = frm;
index = 1;
frame.setQuestion(question[index]);
}

@Override
public void run() {
int option = 0;
try {
is = new BufferedReader(new InputStreamReader(s.getInputStream()));
os = new PrintWriter(s.getOutputStream());

} catch (IOException e) {
System.out.println("IO error in server thread:" + e);
}
try {
while (option > -1) {
try {
option = Integer.parseInt(is.readLine());
os.println(answer[index] == option);
os.flush();
} catch (NumberFormatException e) { //to handle null value
}
System.out.println(result(option));
frame.output(result(option));
}
} catch (IOException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
}

}

String result(int op) {
if (op == -1) {
return "Client exited";
}
if (answer[index] == op) {
return "Option " + op + " is the correct answer.";
} else {
return "Option " + op + " is incorrect.";
}
}

void nextQues() {
index++;
frame.setQuestion(question[index]);
os.println(-2);
os.flush();
}
}

编辑:使用 List<ServerThread>解决了问题。

最佳答案

你的服务器只有一个ServerThread变量,因此只能将数据发送到最后一个添加的一个套接字。相反,考虑给类(class)一个 List<ServerThread>变量以允许它与所有客户端通信。然后在创建连接的 while 循环中,将每个新创建的 ServerThread 添加到此列表中。

您还需要修复服务器的线程问题,以便while (true)循环不会阻塞关键代码。

您还需要升级 ServerThread 类,以便主服务器对象可以与其流进行通信。

此外,您几乎永远不想让一个类扩展 Thread。让它实现 Runnable 来代替。

关于Java:将数据从服务器发送到监听套接字的所有客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39438035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com