Java Multipleclients - server javafx

别等时光非礼了梦想. 提交于 2020-06-01 07:40:31

问题


I have to create an electronic mail with JavaFX using socket, FXML. I need to create one server and 3 clients that are my three accounts and they must start in parallel. Every client must have an associated thread but my problem is: when I start the first client it works, so the FXML file opens. But when I try to open the second client Intellij shows a pop-up that says to me: Stop And Rerun. In my FXML I have a connect button in which I must choose one of my accounts and then my server says "connect". How can I fix this problem? Opening more than one client? If you don't understand I'll try to be more specific.

MailClient.java

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MailClient extends Application {

    @Override
    public void start(Stage stage) {
        try{
            Parent root = FXMLLoader.load(getClass().getResource("FXMLMailClient.fxml"));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();

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

    public static void main(String[] args) throws IOException {
        Socket s = new Socket("127.0.0.1", 4445);
        PrintWriter out = new PrintWriter(s.getOutputStream(), true);
        launch(args);
    }
}
Server.java

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
    private int port = 4445;
    private ServerSocket s = null;

    private static ArrayList<ServerThread> clients = new ArrayList<>();
    private static ExecutorService pool = Executors.newFixedThreadPool(3);

    public void activate() throws IOException {
        try {
            s = new ServerSocket(port);
            while (true) {
                Socket s1 = s.accept();
                System.out.println("Server connect");
                ServerThread st1 = new ServerThread(s1);
                clients.add(st1);

                pool.execute(st1);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }finally{
            s.close();
        }

    }

    public static void main(String[] args) throws IOException {
        Server s = new Server();
        s.activate();
    }
}
ServerThread.java

import java.io.IOException;
import java.net.Socket;

class ServerThread implements Runnable {

    private Socket socket = null;

    public ServerThread(Socket socket)  throws IOException {
        this.socket = socket;
    }
    @Override
    public void run() {
        System.out.println("Connected");
        // during the run, the following cases will be handled:
        // write an email, receive an email, delete an email.
    }
}
FXMLMailClientController.java

import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;

public class FXMLMailClientController {
    private boolean isConnected = false;

    @FXML
    private void handleConnectAction(ActionEvent event) throws IOException {
        while (!isConnected) {
            System.out.println("Client connect");
            isConnected = true;
        }
    }
}

FXMLMailClient.fxml

<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="583.0" prefWidth="994.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.FXMLMailClientController">
    <children>
        <Button fx:id="connectClient" layoutX="70.0" layoutY="116.0" mnemonicParsing="false" onAction="#handleConnectAction" prefWidth="85.0" text="Connetti" />
        <Label fx:id="account" layoutX="383.0" layoutY="14.0" prefWidth="344.0" text="" />
        <ChoiceBox fx:id="choiceAccount" layoutY="83.0" prefHeight="25.0" prefWidth="225.0">
            <items>
                <FXCollections fx:factory="observableArrayList">
                    <String fx:value="email1" />
                    <String fx:value="email2" />
                    <String fx:value="email3" />
                </FXCollections>
            </items>
        </ChoiceBox>
    </children>
</AnchorPane>

来源:https://stackoverflow.com/questions/62117876/java-multipleclients-server-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!