需要java多线程套接字编程的帮助吗

brtdzjyr  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(136)

我正在制作一个服务器和客户机,其中许多客户机可以与服务器连接。服务器将询问客户机一个问题,每个客户机将回答。第一个回答正确的将得到一个分数,服务器操作员将向所有客户机提供新问题。服务器将告诉所有拥有当前最高分数的客户端。任何时候,任何客户都可以加入或离开。我的程序几乎做了所有这些,但是在服务器得到正确的答案之后,因为丢弃了从等待应答的客户端得到答案的线程,我不知所措!快来帮忙!

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;
import java.util.Scanner;

public class Server {

    public static LinkedList<clientThread> clientList = new LinkedList<>();
    static int high = 0;
    static String champ = "";
    static Thread t;
    public static void main(String[] args) {

        try {           
            ServerSocket serverSocket = new ServerSocket(20);
            System.out.println("Server Started");
            System.out.println("Waiting for clients.");
            new Thread() {

                @Override
                public void run() {

                    super.run();
                    Scanner consoleInput = new Scanner(System.in);

                    while(true) {
                        System.out.println("Enter question: ");
                        String qs = consoleInput.nextLine();

                        for (int i = 0; i < clientList.size(); i++) {
                            clientList.get(i).writer.println(qs);
                        }                       

                        for (int i = 0; i < clientList.size(); i++) {
                            final int temp = i;
                            t = new Thread() {
                                public void run() {
                                    System.out.println(clientList.get(temp).name + " answered: " + clientList.get(temp).reader.nextLine());
                                    System.out.println("Enter 'Y' to mark. 'N' to continue.");
                                    char tempChar = consoleInput.nextLine().charAt(0);
                                    while(tempChar != 'Y' && tempChar != 'y' && tempChar != 'N' && tempChar != 'n') {
                                        System.out.println("Wrong input\nEnter again: ");
                                        tempChar = consoleInput.nextLine().charAt(0);
                                    }

                                    if(tempChar == 'Y' || tempChar == 'y') {
                                        clientList.get(temp).score++;
                                    }
                                    t.interrupt();

                                }
                            };
                            t.start();
                        }

                        try {
                            t.join();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        for (int i = 0; i < clientList.size(); i++) {
                            if(high < clientList.get(i).score) {
                                high = clientList.get(i).score;
                                champ = clientList.get(i).name;
                            }
                        }

                        for (int i = 0; i < clientList.size(); i++) {
                            clientList.get(i).writer.println("Current math champ: " + champ + "(" + high + ")");
                        }
                    }
                }
            }.start();

            while(true) {
                clientList.add(new clientThread(serverSocket.accept()));
                clientList.peekLast().name = clientList.peekLast().reader.nextLine();
                System.out.println(clientList.peekLast().name + " joined.");
                clientList.peekLast().start();
            }
        } catch (IOException e) {
            System.out.println("Sorry! Can not start the server");
        }
    }
}
class clientThread extends Thread {

    Socket clientSocket;
    Scanner reader;
    PrintWriter writer;
    int score;
    String name;

    public clientThread(Socket clientSocket) throws IOException {

        this.clientSocket = clientSocket;
        this.reader = new Scanner(clientSocket.getInputStream());
        this.writer = new PrintWriter(clientSocket.getOutputStream(), true);
        this.score = 0;
        this.name = "";
    }

    @Override
    public void run() {

        try {
            clientSocket.isConnected();
        } catch (Exception e) {
            System.out.println("Client Left");
        }
        Server.clientList.remove(this);
    }
}
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) {

        try {

            Socket server = new Socket("localhost", 20);
            Scanner reader = new Scanner(server.getInputStream());
            PrintWriter writer = new PrintWriter(server.getOutputStream(), true);
            Scanner consoleInput = new Scanner(System.in);
            System.out.println("Enter name: ");
            writer.println(consoleInput.nextLine());

            new Thread() {

                public void run() {

                    while(true) {

                        System.out.println("Question: " + reader.nextLine());
                        System.out.println("Enter answer: ");
                        writer.println(consoleInput.nextLine());
                        System.out.println(reader.nextLine());
                    }
                };
            }.start();          

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题