一旦将启动(args)-Method-call添加到main-Method,客户端将不会连接到服务器

时间:2019-05-31 23:45:35

标签: java javafx client-server

我想建立一个客户端-服务器连接(客户端上具有Javafx接口),但是客户端不会连接到服务器。有趣的是,当Main方法中的launch(args)-Method-Call被注释掉时,客户端将连接到服务器。

我对Java特别是Javafx和Client-Server-Applications很陌生。 以下代码部分不能代表整个类,我只是省略了上下文中不必要的方法(或者至少我认为它们是不必要的。

Main.java

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        LoginUI loginUI = new LoginUI(primaryStage);
        loginUI.setScene(primaryStage, loginUI.getScene());
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
        try {
            Client client = new Client(InetAddress.getLocalHost(), 9376);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        //loginUI.getSignUpUI().setSignUpButtonAction(client);
        //my only Instace of LoginUI is in start-Method. How do I pass that to main, since main is static?
    }
}

Client.java

public class Client {

    private Socket clientSocket;
    private DataOutputStream outputClient;
    private DataInputStream inputClient;

    public Client(InetAddress ipAddress, int port){
        try {
            this.clientSocket = new Socket(ipAddress, port);
            this.outputClient = new DataOutputStream(clientSocket.getOutputStream());
            this.inputClient = new DataInputStream(clientSocket.getInputStream());
            outputClient.writeUTF("Client connected on port: " + clientSocket.getPort() + ", with IP: " + clientSocket.getInetAddress());
        }
        catch(IOException e3){
            e3.printStackTrace();
        }
    }
}

Server.java

public class Server {

    private ServerSocket skt;
    private volatile boolean serverON;
    private Socket clientSocket;

    public Server(int port) {
        try {
            this.skt = new ServerSocket(port);
            this.serverON = true;
        }
        catch(IOException e1){
            e1.printStackTrace();
        }
    }

    public void serverStart() {
        while (serverON) {
            clientSocket = null;
            System.out.println("Waiting for client on port " + skt.getLocalPort());
            try {
                clientSocket = skt.accept();
                System.out.println("Client has connected: " + clientSocket);

                DataInputStream dataIn = new DataInputStream(clientSocket.getInputStream());
                DataOutputStream  dataOut = new DataOutputStream(clientSocket.getOutputStream());
                System.out.println("Trying to assign Thread to client...");

                Thread thread = new ClientHandler(clientSocket, dataIn, dataOut);
                thread.start();

            }
            catch (Exception e) {
                try {
                    clientSocket.close();
                }
                catch (IOException e1){
                    e1.printStackTrace();
                }
                e.printStackTrace();
                break;
            }
        }
    }
}

ServerMain.java

public class ServerMain {

    public static void main(String[] args){
        Server server = new Server(9376);
        server.serverStart();
    }
}

当没有注释掉launch()调用时,我没有收到任何错误(至少最初是错误的,以后以特定方式使用UI时仍然会发生错误),客户端不会连接到服务器。

0 个答案:

没有答案