Answered You can hire a professional tutor to get the answer.

QUESTION

Please create chat window application using Javafx and the client server code. ====================================== the following is my code is

Please create chat window application using

Javafx and the client server code.

======================================

the following is my code is there anyone to check my code and correct any issues?

thanks

---------------------------------

ChatApp.java

------------------------

import javafx.application.Application;

import javafx.scene.Parent;

import javafx.scene.scene;

import javafx.scene.control.TextArea;

import javafx.scene.control.TextField;

import javafx.scene..layout.VBox;

import javafx.stage.Stage;

  pub class ChatApp extends Application {

private boolean isServer=true;

private TextArea messages=new TextArea();

private NetworkConnection=isServer? createServer(): createClient();

private Parent createContent(){

 message.setPrefHeight(550);

 TextField input=new TextField();

input setOnAction( event -> {

string message=isServer ? "server"="Client";

 message + input.getText();

 input.clear;

messages.append(message + "n");

try {

connection.send(message);

}

catch (Exception e){

messages.appendText("failed to sendn"

    e.printStackTrace();

  TextField input= new TextArea();

      VBox root=new =new VBox(20, messages, input);

  root.setPrefSize(600,600)'

      return root;

  }

public void int() throws Exception {

connection.startConnection();

}

  public void start(stage primary stage) throws exception {

primaryStage.setScene(new Scene(create content()));

primaryStage.show();

  }

public void stop() throws Exception{

connectioncloseConnection();

private Server createServer(){

return new server(55555, data ->{

platformrunLater(() ->{

message.AppendText(data.toString() + "n";

  }};

 }};

}

private Client createClent(){

 return new Client("127.0.0.1", 55555,data->)

public static void main(String[] args{

    Launch(args);

  }

}

Connection.java

---------------

import.java.io.Serializable;

import.java.net.Server.Socket;

import.java.util.function.consumer;

 public abstract class NetworkConnection {

  private ConnectionThread onThread= new ConnectionThread();

  private Consumer<Serilizable> onReceiveCallback){

   this.onReceiveCallback=onReceiveCallback;

  }

public void startConnection() throws Exception{

  connThread.start();

}

public void send(Serilizable data) throws Exception{

 connThread.out.writeObject(data);

}

public void closeConnection(Serilizable data) throws Exception{

 connThread.socket.close();

}

protected abstract boolean isServer();

protected abstract String getIp();

protected abstract int getPort();

private class ConnectionThread extends Thread{

private Socket socket;

private ObjectStream out;

  public void run(){

try (Server socket server=isServer() ? new ServerSocket(getPort()):null;

       Socket socket=isServer() ? server.accept(): new Socket(getIp(),getPort());

       ObjectOutputStream out=new ObjectOutputStream(socket.getOutputStraem());

   ObjectInputStream in =new ObjectInputStream(socket.getInputStream())){

this.socket=socket;

this.out=out;

socket.setTcpNoDelay(true);

while(true){

Serilizable data=(serilizable)in.readObject();

onReceiveCallback.accept(data);

}

}

catch (Exception e){

onReceiveCallback.accept("Connection closed");

  }

 }

}

------------------------

server.java

-----------------------

package com.almsb.chat;

import java.io.Serizable;

import java.util.function.Consumer;

public class Server extends NetworkConnection {

private int port;

}

 protected boolean isServer(){

return true;

}

protected String getIp(){

return null;

}

protected int getPort(){

return port;

 }

}

-----------------------------

package com.almsb.chat;

import java.io.Serizable;

import java.util.function.Consumer;

public class Server extends NetworkConnection {

private String ip;

private int port;

this ip=ip;

this port=port;

 public Client(String ip, int portConsumer.Serizable onReceiveCallback){

super(onReceiveCallback);

}

 protected boolean isServer(){

 return false;

}

protected String getIp(){

return ip;

}

protected int getPort(){

return port;

}

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question