I'm developing a Javafx application and I need that clients run only when so server does first. I'm a newbie to socket and concurrent programming and I tried to send from server a string called "Done" when is closed. However, I don't know how to catch the string properly in order to throw the right exception for the closure of the server when the clients are running, because otherwise the clients in execution run even without the server after is stopped and I want to prevent it. Thanks for your help!
That's my client (InitApplication.java)
package com.sailingclub.ui.sailingclub;
import body.entities.Admin;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import static java.lang.System.exit;
/**
* Entry point
*/
public class InitApplication extends Application {
DataOutputStream toServer = null;
DataInputStream fromServer = null;
Parent root = null;
FXMLLoader loader;
@Override
public void start(Stage primaryStage) throws IOException, InterruptedException {
try {
// Create a socket to connect to the server
Socket socket = new Socket("localhost", 8000);
fromServer = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
toServer = new DataOutputStream(socket.getOutputStream());
primaryStage.setOnCloseRequest(event -> {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
// Save file
});
loader = new FXMLLoader(new File("src/main/java/com/sailingclub/ui/sailingclub/first/FirstPage.fxml").toURI().toURL());
root=loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
if (e instanceof ConnectException) {
loader = new FXMLLoader(new File("src/main/java/com/sailingclub/ui/sailingclub/error.fxml").toURI().toURL());
root = loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
else {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
var beginning=new Admin();
beginning.notifyGenerator("Membership");
beginning.notifyGenerator("Storage");
launch();
}
}
and that's my server (Server.java)
package body.core.ClientServer;
import com.sailingclub.ui.sailingclub.InitApplication;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
/**
* Server side for Client-Server connection
*/
public class Server extends Application {
@Override
public void start(Stage stage) throws Exception {
TextArea ta = new TextArea();
// Create a scene and place it in the stage
Scene scene = new Scene(new ScrollPane(ta), 450, 200);
stage.setTitle("Server"); // Set the stage title
stage.setScene(scene); // Place the scene in the stage
stage.show(); // Display the stage
new Thread( () -> {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
Platform.runLater(() ->
ta.appendText("Server started at " + new Date() + '\n'));
// Listen for a connection request
Socket socket = serverSocket.accept();
// Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(
socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(
socket.getOutputStream());
// while (true) {
// Send result back to the client
//outputToClient.writeInt(flag);
// }
stage.setOnCloseRequest(event -> {
try {
outputToClient.writeBytes("Done");
serverSocket.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
// Save file
});
}
catch(IOException ex) {
ex.printStackTrace();
}
}).start();
/* InitApplication client = new InitApplication();
Stage clientStage = new Stage();
clientStage.initOwner(stage);
client.start(clientStage); */
}
public static void main(String[] args)
{
launch();
}
}