Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Client Help Needed: I have a button that is supposed to send the text to the text area box. (That works.) The other thing

Java Client Help Needed:

I have a button that is supposed to send the text to the text area box. (That works.) The other thing it is supposed to do is to send the text to the server. I'm confused on how to do that.

import java.io.*; import java.net.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException;

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.stage.Stage;

public class EchoClient extends Application {

static Socket soc;

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

// String hostName = ChatServer; // int portNumber = Integer.parseInt(args[1]);

// Variable block

String clientName = "Anonymous"; // Default client name int clientPort = 4600; // Default port name

if(args.length >0) // If there is a name argument:

{ // opens if statement

//Reading in the 0 argument and storing it in variable clientName

clientName = args[0];

}

if(args.length > 1) //If there is a port argument: {

// Reading in 1 argument and storing it in the variable clientPort

clientPort = Integer.parseInt(args[1]); // Parsing the port string to an integer

} // Closes if statement

// System.out.println (clientName); // Testing args by printing out to concsole. // System.out.println (clientPort); try ( Socket mySocket = new Socket(clientName, clientPort); PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(mySocket.getInputStream())); BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)) ) { String userInput; while ((userInput = stdIn.readLine()) != null) { soc = mySocket; out.println(userInput); System.out.println(in.readLine()); } } catch (UnknownHostException e) { System.err.println("Don't know about host " + clientName); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to " + clientName); System.exit(1); } } // Variable Block

Button btnDisconnect, btnSend; // Button Variables TextArea myTextArea; HBox btnbox; TextField clientChat; String chatText = "Chat will appear here; "; // String defaultChat = "Enter Messages here: ";

public void start(Stage primaryStage) throws Exception { //Opens start

//Create buttons btnDisconnect = new Button("Disconnect"); btnSend = new Button ("Send");

// Add event handlers to buttons btnDisconnect.setOnAction(e->handleButtonAction(e)); btnSend.setOnAction(e->handleButtonAction(e));

//Text Area Scroller

ScrollPane scrollPane = new ScrollPane(myTextArea); scrollPane.setContent(myTextArea); scrollPane.setFitToWidth(true); scrollPane.setPrefWidth(400); scrollPane.setPrefHeight(180);

btnbox=new HBox(btnDisconnect, btnSend); btnbox.setSpacing(10); btnbox.setPadding(new Insets(20));

myTextArea = new TextArea(chatText); myTextArea.setWrapText(true); clientChat = new TextField ();

// Create a Border Pane BorderPane root = new BorderPane(); root.setPadding(new Insets(20));

BorderPane.setAlignment(clientChat,Pos.BOTTOM_LEFT);

// Position components

root.setTop(btnbox); root.setCenter(myTextArea); root.setBottom(clientChat);

// Create a scene! Scene scene = new Scene(root, 450, 250);

primaryStage.setTitle("Chat Room"); primaryStage.setScene(scene); primaryStage.show();

} // Closes start private void handleButtonAction(ActionEvent event) { //Opens handleButton

if(event.getSource()==btnSend){ String TextField = clientChat.getText(); myTextArea.appendText(" "); myTextArea.appendText(TextField); clientChat.clear(); }

else if(event.getSource()==btnDisconnect){

try{ soc.close(); System.exit(1); }catch(Exception e){ System.out.println(e.getMessage()); } System.exit(1); }

} // Closes handleButton

} // Closes Class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions