【问题标题】:Multithread Server and Client with sockets in JavaJava中带有套接字的多线程服务器和客户端
【发布时间】:2021-02-18 00:27:50
【问题描述】:

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

我为客户端设置了 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();
}

【问题讨论】:

  • 您调用了“out.close()”,因此您的套接字服务器没有问题地关闭。从客户端发送随机数到服务器后,您想收到相同的数字吗?
  • out.close() 位于 for 循环之外,由于许多更改,我将其留在了 for 循环中。但这不会导致问题。在最好的情况下,我只从服务器获得第一个数字。我在调试器上注意到的是 PrintWriter 有一个标志问题,这是真的,我不知道是什么导致了这种变化。
  • 你会提交并推送你的代码到 GitHub,然后分享地址吗?我想我们最好讨论一下代码和逻辑。
  • 您正在关闭套接字,而线程仍在运行。解决方案:不要。收到流结束时在读取线程中关闭它。

标签: java multithreading client-server


【解决方案1】:

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

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();
        }
    }

【讨论】:

  • 这不是问题。现在,您正在浪费地为每个逻辑连接创建两个套接字。
  • 我什至尝试过不关闭连接,只有当我在线程内创建套接字时,我才设法解决它。怎么可能解决?
  • 四天前,我已经在 cmets 中根据您的问题告诉过您。
猜你喜欢
  • 1970-01-01
  • 2011-10-17
  • 2016-05-17
  • 2017-02-25
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 2010-12-19
  • 2013-05-14
相关资源
最近更新 更多