So, I made a simple server-client chat program, but I have a problem:
if the server tries to message the client, the server lags or freezes a bit, same with the client.
is there something that I do wrong?
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
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 Group serverGR;
private Scene serverSC;
private Stage window;
private TextField tf;
private TextArea ta;
private ServerSocket ss;
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
ConnectionThread cT = new ConnectionThread();
public void init() {
cT.start();
}
@Override
public void start(Stage primaryStage) {
try {
serverGR = new Group();
serverSC = new Scene(serverGR, 800, 500, Color.LIGHTCYAN);
window = primaryStage;
window.setTitle("Bleidorb Simplistic ChatProgram \\\\\\\\\"ServerMode\"\\\\\\\\");
window.setScene(serverSC);
window.show();
tf = new TextField();
tf.setTranslateX(10);
tf.setTranslateY(10);
tf.setPrefWidth(400);
tf.setPrefHeight(20);
tf.appendText("Type your message here.");
tf.setOnAction(event -> {
String server = "Server: ";
server += tf.getText();
try {
dos.writeUTF(server);
dos.flush();
ta.appendText(dis.readUTF() + "\n");
} catch (IOException e) {
e.printStackTrace();
}
tf.clear();
});
ta = new TextArea();
ta.setTranslateX(10);
ta.setTranslateY(40);
ta.setPrefWidth(400);
ta.setPrefHeight(400);
serverGR.getChildren().addAll(tf, ta);
} catch (Exception e) {
System.out.println(e.toString() + "\n something wrong in startmethod, lol?");
}
}
@SuppressWarnings("deprecation")
public void stop() {
try {
cT.stop();
dis.close();
ss.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
private class ConnectionThread extends Thread {
@Override
public void run(){
try {
ss = new ServerSocket(5279, 100);
s = ss.accept();
s.setTcpNoDelay(true);
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Client extends Application {
private Group serverGR;
private Scene serverSC;
private Stage window;
private TextField tf;
private TextArea ta;
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
ConnectionThread cT = new ConnectionThread();
public void init() {
cT.start();
}
@Override
public void start(Stage primaryStage) {
try {
serverGR = new Group();
serverSC = new Scene(serverGR, 800, 500, Color.LIGHTCYAN);
window = primaryStage;
window.setTitle("Bleidorb Simplistic ChatProgram \\\\\\\\\"ClientMode\"\\\\\\\\");
window.setScene(serverSC);
window.show();
tf = new TextField();
tf.setTranslateX(10);
tf.setTranslateY(10);
tf.setPrefWidth(400);
tf.setPrefHeight(20);
tf.appendText("Type your message here.");
tf.setOnAction(event -> {
String client = "Client: ";
client += tf.getText();
try {
dos.writeUTF(client);
dos.flush();
ta.appendText(dis.readUTF() + "\n");
} catch (IOException e) {
e.printStackTrace();
}
tf.clear();
});
ta = new TextArea();
ta.setTranslateX(10);
ta.setTranslateY(40);
ta.setPrefWidth(400);
ta.setPrefHeight(400);
serverGR.getChildren().addAll(tf, ta);
} catch (Exception e) {
System.out.println(e.toString() + "\n something wrong in startmethod, lol?");
}
}
@SuppressWarnings("deprecation")
public void stop() {
try {
cT.stop();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
private class ConnectionThread extends Thread {
@Override
public void run(){
try {
s = new Socket("localhost", 5279);
s.setTcpNoDelay(true);
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}