Posted on Thursday, October 31, 2019
These are 2 main steps on how I build JavaFX Chat application using socket programming
Output:
Download: JavaFXSocketProgramming .
It is better to download it now and check the file line by line while i will explain the code in the following paragraph.
Explanation:
//ServerJavaFx.java starting from line 52-77
//create a new thread
new Thread(() -> {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(ConnectionUtil.port);
//append message of the Text Area of UI (GUI Thread)
Platform.runLater(()
-> txtAreaDisplay.appendText("New server started at " + new Date() + '\n'));
//continous loop
while (true) {
// Listen for a connection request, add new connection to the list
Socket socket = serverSocket.accept();
TaskClientConnection connection = new TaskClientConnection(socket, this);
connectionList.add(connection);
//create a new thread
Thread thread = new Thread(connection);
thread.start();
}
} catch (IOException ex) {
txtAreaDisplay.appendText(ex.toString() + '\n');
}
}).start();
//line starting at 88-93
//send message to all connected clients
public void broadcast(String message) {
for (TaskClientConnection clientConnection : this.connectionList) {
clientConnection.sendMessage(message);
}
}
//TaskClientConnection.java, starting line 41-46
while (true) {
// Get message from the client
String message = input.readUTF();
//send message via server broadcast
server.broadcast(message);
//server broadcast method send message to all connected clients.
//clients are stored in List<> variable.
//ClientJavaFx.java, starting line at 85-102
try {
// Create a socket to connect to the server
Socket socket = new Socket(ConnectionUtil.host, ConnectionUtil.port);
//Connection successful
txtAreaDisplay.appendText("Connected. \n");
// Create an output stream to send data to the server
output = new DataOutputStream(socket.getOutputStream());
//create a thread in order to read message from server continuously
TaskReadThread task = new TaskReadThread(socket, this);
Thread thread = new Thread(task);
thread.start();
} catch (IOException ex) {
txtAreaDisplay.appendText(ex.toString() + '\n');
}
//starting line at 115-135
private class ButtonListener implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
try {
//get username and message
String username = txtName.getText().trim();
String message = txtInput.getText().trim();
//if username is empty set it to 'Unknown'
if (username.length() == 0) {
username = "Unknown";
}
//if message is empty, just return : don't send the message
if (message.length() == 0) {
return;
}
//send message to server
output.writeUTF("[" + username + "]: " + message + "");
output.flush();
//TaskReadThread.java, starting line at 31-46
@Override
public void run() {
//continuously loop it
while (true) {
try {
//Create data input stream
input = new DataInputStream(socket.getInputStream());
//get input from the client
String message = input.readUTF();
//append message of the Text Area of UI (GUI Thread)
Platform.runLater(() -> {
//display the message in the textarea
client.txtAreaDisplay.appendText(message + "\n");
});
5 classes explanation:
The chat application was part of our assignments in our university; Charles Sturt University, Sydney. I am just sharing my learning here so that it could be helpful to you.
Following site (console application) was the most helpful resource where I research through and got understanding and build chat application in JavaFX: codejava.net resource