Question: Java Sockets I'm trying to have my chat program disconnect from the server when the user presses a button. I'm getting an error when using

Java Sockets

I'm trying to have my chat program disconnect from the server when the user presses a button. I'm getting an error when using the close() method from the sockets class. I know its a scope issue, but I don't know how I should have set it up. Here's the code:

package application;

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 chatTester extends Application { // opens class

public static void main(String[] args) { // opens main

launch(args);

// 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) { out.println(userInput); System.out.println("echo: " + 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); }

} // Closes main

// 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(TextField); clientChat.clear();

}

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

mySocket.close(); System.exit(1); }

} // Closes handleButton

} //closes class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!