I am making a program, and I don't know why my program becomes unresponsive, when I click the button ):
I have a feeling its because of this:
"connection = server.accept();"
I don't know how to fix it.
I am trying to recreate this:
https://stackoverflow.com/questions/18505123/how-to-use-client-chat-on-different-pc
but in javafx.
package yeetYeet;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Server extends Application{
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
//set up and run the server
private void startRunning() {
try{
server = new ServerSocket(6789, 100);
new AnimationTimer() {
@Override
public void handle(long arg0) {
try{
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Server ended the connection! ");
}catch (IOException IOException){
IOException.printStackTrace();
}finally{
closeCrap();
}
}
}.start();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//wait for connection, then display connection information
private void waitForConnection() throws IOException {
showMessage(" Waiting for someone to connect... \n");
connection = server.accept();
showMessage(" Now connected to " + connection.getInetAddress().getHostName());
}
//get stream to send and receive data
private void setupStreams() throws IOException {
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are now setup! \n");
}
//during the chat conversation
private void whileChatting() throws IOException {
String message = " You are now connected! ";
sendMessage(message);
ableToType(true);
do {
try {
message = (String) input.readObject();
showMessage("\n" + message);
}catch(ClassNotFoundException classNotFoundException) {
showMessage("\n idk wtf that user sent!");
}
}while(!message.equals("CLIENT - END"));
}
//close streams and sockets after you are done chatting
private void closeCrap() {
showMessage("\n Closing connections... \n");
ableToType(false);
try {
output.close();
input.close();
connection.close();
}catch(IOException ioException) {
ioException.printStackTrace();
}
}
//send a message to client
private void sendMessage(String message) {
try {
output.writeObject("SERVER - " + message);
output.flush();
showMessage("\nSERVER - " + message);
}catch(IOException ioException) {
ta.appendText("\n ERROR: DUDE I CANT SEND THAT MESSAGE");
}
}
//updates chatWindow
private void showMessage(final String text) {
new AnimationTimer() {
@Override
public void handle(long arg0) {
ta.appendText(text);
}
}.start();
}
//let the user type stuff into their box
private void ableToType(final boolean tof) {
new AnimationTimer() {
@Override
public void handle(long arg0) {
tf.setEditable(tof);
}
}.start();
}
private Group serverGR;
private Scene serverSC;
private Stage window;
private TextField tf;
private TextArea ta;
private Button connectingStart;
@Override
public void start(Stage primaryStage) throws Exception {
serverGR = new Group();
serverSC = new Scene(serverGR, 1280, 720, Color.PALEGREEN);
window = primaryStage;
window.setScene(serverSC);
window.setTitle("Bleidorb -\\\\\"Server\"\\\\- ChatProgram");
window.show();
tf = new TextField();
tf.setTranslateX(10);
tf.setTranslateY(10);
tf.setPrefWidth(500);
tf.setPrefHeight(20);
tf.appendText("type your message here");
serverGR.getChildren().add(tf);
ta = new TextArea();
ta.setTranslateX(10);
ta.setTranslateY(40);
ta.setPrefWidth(500);
ta.setPrefHeight(500);
serverGR.getChildren().add(ta);
connectingStart = new Button("Connection Start");
connectingStart.setTranslateX(600);
connectingStart.setTranslateY(300);
connectingStart.setPrefWidth(150);
connectingStart.setPrefHeight(30);
connectingStart.setOnAction(e -> {
startRunning();
});
serverGR.getChildren().add(connectingStart);
}
public static void main(String[] args) {
launch(args);
}
}