JavaFX chat

I am trying to make chat program in java (javafx as client side). I can not understand why Bufferedreader bReader can not read text that I am sending to server.

client side


package ba.jaso;

import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FXClient extends Application {
	Group root;
	Scene scene;
	TextArea outputTxt;
	TextField inputTxt;

	Socket socket;
	BufferedReader bReader;
	BufferedWriter bWriter;
	PrintWriter out = null;
	List list;

	public FXClient() {
		try {
			socket = new Socket("127.0.0.1", 4444);

			bReader = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			bWriter = new BufferedWriter(new OutputStreamWriter(
					socket.getOutputStream()));
			out = new PrintWriter(socket.getOutputStream(), true);

			Thread th = new Thread();
			th.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void start(Stage stage) {
		root = new Group();
		scene = new Scene(root);
		stage.setScene(scene);
		stage.setResizable(false);

		VBox vbox = new VBox();

		outputTxt = new TextArea();
		outputTxt.setMinSize(400, 400);
		outputTxt.setDisable(false);
		outputTxt.setEditable(false);
		outputTxt.setFocusTraversable(false);

		inputTxt = new TextField();
		inputTxt.setFocusTraversable(true);
		inputTxt.setMinSize(400, 30);
		addTxtHandler();

		vbox.getChildren().add(outputTxt);
		vbox.getChildren().add(inputTxt);

		root.getChildren().add(vbox);

		stage.addEventHandler(MouseEvent.MOUSE_MOVED,
				new EventHandler<MouseEvent>() {

					@Override
					public void handle(MouseEvent arg0) {
						System.out.println(arg0.toString());
						try {

							// while (bReader.readLine() != null) {
							// outputTxt.setText(bReader.readLine());
							// }
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});

		stage.show();
	}

	public void addTxtHandler() {
		inputTxt.addEventHandler(KeyEvent.KEY_PRESSED,
				new EventHandler<KeyEvent>() {

					@Override
					public void handle(KeyEvent e) {
						if (e.getCode() == KeyCode.ENTER) {
							out.print(inputTxt.getText());
						}
					}
				});
	}

	public static void main(String[] args) {
		launch(args);
	}

}


server side code


/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package javaservertest;

/**
 *
 * @author jasmin
 */
import java.lang.*;
import java.io.*;
import java.net.*;

class Server {

    public static void main(String args[]) {
        String data = "Toobie ornaught toobie";
        try {
            ServerSocket srvr = new ServerSocket(4444);

            Socket skt = srvr.accept();
            BufferedReader reader = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            System.out.print("Server has connected!\n");
            PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
            System.out.print("Sending string: '" + data + "'\n");
            out.print(data);
     
            System.out.println(reader.ready());
            
           while(reader.readLine() != null) { //HERE :S please help
                System.out.println(reader.readLine());
            }
           
            out.close();
            skt.close();
            srvr.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("Whoops! It didn't work!\n");
        }
    }
}


You seem to be creating an empty thread in the FXClient’s constructor. I am not too familiar with JavaFX, could kindly explain the life cycle methods of Application?

Oh sorry, I saw empty thread after I made this post. Anyways, I find where is error.
btw thx for reading code and trying to help :wink: