带有 Java 套接字的多线程服务器和客户端

时间:2021-02-18 00:27:50

标签: java multithreading client-server

我正在尝试为大学项目创建服务器/从属/客户端项目。 服务端需要开放2个端口,一个端口用于连接slave,另一个端口用于客户端。

我为客户端设置了 2 个线程,1 个为从站设置了另一个线程。客户端应该向服务器发送随机数,服务器应该将这些数字随机转发到从属实例。从站应该检查当前号码是否存在于他们的列表中,以及是否无法存储它,否则他们应该向服务器发送一条消息,表明该号码已经存在。

然后我创建了由 2 个线程组成的客户端线程,一个用于将数字发送到服务器,另一个用于读取来自服务器的消息。 PrintWriter 的代码有问题,当代码在线程内时,我无法将数字发送到服务器。如果我在主线程上移动代码并取消线程,则消息发送没有任何问题。 这可能是什么问题?

下面是来自服务器(主)和客户端的当前代码。

public class Client {
private static final int NUMBERS = 50;
private static final int AMPLITUDE = 100;
private static int masterPort;

public Client(int port) {
    this.masterPort = port;
}

public static void main(String[] args) throws IOException{
    String serverHostname = "127.0.0.1"; 

    System.out.println("Αναμονή για σύνδεση στον σέρβερ " + serverHostname + " στην πόρτα 30091.");
    Socket echoSocket = null;
    BufferedReader in = null;
    try {
        echoSocket = new Socket(serverHostname, 18889); 
        in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); 
    } catch (UnknownHostException e) {
        System.err.println("Δεν μπορεί να πραγματοποιηθεί σύνδεση με τον σέρβερ: " + serverHostname); 
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: " + serverHostname); 
        System.exit(1);
    }

    ClientOut clientOut = new ClientOut(echoSocket);
    clientOut.start();
    ClientIn clientIn = new ClientIn(in);
    clientIn.start();

    in.close();
    echoSocket.close();
}

public static class ClientOut extends Thread {
    private PrintWriter out;

    public ClientOut(Socket echoSocket) throws IOException {
        this.out = new PrintWriter(echoSocket.getOutputStream(), true);
    }

    @Override
    public void run() {
        System.out.println("Ο client συνδέθηκε!");
        Random rnd = new Random();
        try {
            for (int i=0; i<NUMBERS; i++) {
                int num = rnd.nextInt(AMPLITUDE);
                System.out.println(num);
                out.println(num);
                TimeUnit.SECONDS.sleep(1);
            }
        } catch (InterruptedException e) {
                e.printStackTrace();
            }
                out.close();
        }
    }


public static class ClientIn extends Thread {
    private BufferedReader in;

    public ClientIn(BufferedReader in) {
        this.in = in;
    }

    @Override
    public void run() {

    }
}

}

public class Master {
private int slavePort;
private int clientPort;
private SlaveThread slaveThread;
private ClientThread clientThread;
private boolean running = false;
public static int slaveConnected; // Slave connection counter

public Master(int slavePort, int clientPort) {
    this.slavePort = slavePort;
    this.clientPort = clientPort;
    this.slaveConnected = 0; 

public void startServer() {
    try {
        this.slaveThread = new SlaveThread(slavePort);
        this.clientThread = new ClientThread(clientPort);
        System.out.println( "Αναμονή για σύνδεση client / slave" );
        slaveThread.start();
        clientThread.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void stopServer() {
    running = false;
    this.slaveThread.interrupt();
    this.clientThread.interrupt();

}

class SlaveThread extends Thread {
    private ServerSocket slaveSocket;

    SlaveThread(int slavePort) throws IOException {
        this.slaveSocket = new ServerSocket(slavePort);
    }
    @Override
    public void run() {
        running = true;
        while (running) {
            try {
                // Call accept() to receive the next connection
                Socket slSocket = slaveSocket.accept();
                System.out.println("Δημιουργήθηκε μια νέα σύνδεση Slave");

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

class ClientThread extends Thread {
    private ServerSocket clientSocket;

    ClientThread(int clientPort) throws IOException {
        this.clientSocket = new ServerSocket(clientPort);
    }

    @Override
    public void run() {
        running = true;
        while (running) {
            try {
                Socket clSocket = clientSocket.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(clSocket.getInputStream()));
                System.out.println("Δημιουργήθηκε μια νέα σύνδεση Client");

                String inputLine;
                while ((inputLine = in.readLine())  != null) {
                    System.out.println("Client: " + inputLine);
                }

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

public static void main(String[] args) {
    Master server = new Master( 30091, 18889);
    server.startServer();
    // Automatically shutdown in 1 minute
    try {
        Thread.sleep( 60000 );
    } catch(Exception e) {
        e.printStackTrace();
    }
    server.stopServer();
}

1 个答案:

答案 0 :(得分:-1)

我找到了解决方案。 Socket 应该在 Client Thread 构造函数上创建,而不是作为引用传递。 所以客户端应该是

public class Client {
private static final int NUMBERS = 50;
private static final int AMPLITUDE = 100;
private static int masterPort;

public Client(int port) {
    this.masterPort = port;
}

public static void main(String[] args) throws IOException{
    String serverHostname = "127.0.0.1"; //Ορίζουμε την διεύθυνση που είναι ο σέρβερ

    System.out.println("Αναμονή για σύνδεση στον σέρβερ " + serverHostname + " στην πόρτα 30091.");

    ClientOut clientOut = new ClientOut(serverHostname);
    clientOut.start();
    ClientIn clientIn = new ClientIn(serverHostname);
    clientIn.start();
}

public static class ClientOut extends Thread {
    private Socket echoSocket;
    private PrintWriter writer;

    ClientOut(String serverHostname) throws IOException {
        this.echoSocket = new Socket(serverHostname, 18889);
        this.writer = new PrintWriter(echoSocket.getOutputStream(), true);;
    }

    @Override
    public void run() {
        System.out.println("Ο client συνδέθηκε!");
        Random rnd = new Random();
        try {
            for (int i=0; i<NUMBERS; i++) {
                int num = rnd.nextInt(AMPLITUDE);
                System.out.println(num);
                writer.println(num);
                TimeUnit.SECONDS.sleep(1);
            }
        } catch (InterruptedException e) {
                e.printStackTrace();
            }
        writer.close();
        }
    }
相关问题