Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中带套接字的多线程服务器和客户端_Java_Multithreading_Client Server - Fatal编程技术网

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

Java中带套接字的多线程服务器和客户端,java,multithreading,client-server,Java,Multithreading,Client Server,我正在尝试为一个大学项目创建一个服务器/从机/客户端项目。 服务器应打开2个端口,一个端口用于连接从机,另一个端口用于连接客户端 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 = po

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

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();
}
我设置了两个线程,一个用于客户端,另一个用于从机。客户端应向服务器发送随机数,服务器应将这些随机数转发给从属实例。从属服务器应检查其列表中是否存在当前号码,以及是否无法存储该号码,否则应向服务器发送一条消息,说明该号码已存在

然后我创建了客户端线程,它由两个线程组成,一个用于向服务器发送数字,另一个用于读取来自服务器的消息。 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();
}
我找到了解决办法。 套接字应该在客户端线程构造函数上创建,而不是作为引用传递。 所以客户应该

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

您调用了“out.close”,因此套接字服务器已顺利关闭。在将随机数从客户端发送到服务器后,是否要接收相同的数字?out.close在for循环之外,因为我将它保留在for循环内进行了许多更改。但这并不是问题的根源。在最好的情况下,我只从服务器获得第一个号码。我在调试器上注意到的是,PrintWriter有一个标志问题,这是真的,我不知道是什么导致了这种更改。您是否会提交代码并推送到GitHub,然后共享地址?我认为我们最好讨论一下代码和逻辑。你可以在关闭套接字时检查它,而线程仍在运行。解决方法:不要。当接收到流结束时,在读取线程中关闭它。这不是问题所在。现在,您正在浪费时间为每个逻辑连接创建两个套接字。我甚至尝试不关闭连接,只有在线程内创建套接字时,我才设法解决它。怎么可能解决呢?四天前,我已经在你的问题下的评论中告诉你了。