首页 > 解决方案 > 为什么 javafx 应用程序在同时使用 EventHandler 和 java.net 套接字时崩溃?

问题描述

我正在尝试使用 Javafx 和 java.net 网络包构建一些聊天应用程序。我希望用户在文本字段中键入的文本从客户端发送到服务器,然后从那里返回到客户端并最终显示在应用程序的文本区域中。但是,当我在 TextField 中输入文本时按 Enter 后,应用程序崩溃了。所以在 EventHandler 的“句柄”方法中肯定有错误。此外,应用程序在输入文本被清除之前崩溃。应用程序没有响应 --> 弹出错误窗口。应用程序没有响应

客户端代码:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.Parent;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.*;

public class ChatAppClient extends Application {

private TextArea messages = new TextArea();
private TextField input;
private static String ipAdress = null;
private DataInputStream dis;
private DataOutputStream dos;
private static Socket client;

public Parent mainScene() throws UnknownHostException, IOException {

  // Getting IP-Adress from a different file as a String

  String userDir = System.getProperty("user.dir");
  File file = new File(userDir + "\\..\\IPAdress.txt");
  RandomAccessFile raFile = new RandomAccessFile(file, "rw");
  raFile.seek(0);
  ipAdress = raFile.readLine();
  raFile.close();

  // Initialising new Socket

  client = new Socket(ipAdress, 5000);
  System.out.println("Connected to Server.");

  // Defining JavaFx Application-Layout

  messages.setPrefHeight(220);
  input = new TextField();

  VBox root = new VBox(20, messages, input);
  root.setPrefSize(400, 400);

  // Setting up action for Textfield when pressing Enter

  input.setOnAction(new EventHandler<ActionEvent>() {

     @Override
     public void handle(ActionEvent event) {

        try {

           // Defining and initialising Input-String

           String message = "Client: ";
           message += input.getText();

           // Opening OutputStream for String to send it to the server
           dos = new DataOutputStream(client.getOutputStream());

           // Giving the String to the OutputStream
           dos.writeBytes(message);

           // Clearing the Textfield from Input-Text
           input.clear();

           // Setting up InputStream for the Socket to receive the String back from the
           // server

           dis = new DataInputStream(client.getInputStream());

           // Reading String from InputStream

           String s = dis.readLine();

           // Closing In-and OutputStreams + Socket

           dos.close();

           // Showing the String in the TextArea "messages"

           messages.appendText(s + "\n");
        } catch (IOException ioE) {
           ioE.printStackTrace();
        }
     }
  });

  return root;

  }

  public void start(Stage mainStage) {

  try {
     mainStage.setScene(new Scene(mainScene()));
     mainStage.show();

  } catch (UnknownHostException uHE) {
     uHE.printStackTrace();
  } catch (IOException iOE) {
     iOE.printStackTrace();
  }

  }

  public static void main(String[] args) {
  try {
     launch(args);
     client.close();
  } catch (IOException ioE) {
     ioE.printStackTrace();
  }
  }

  }

服务器端代码:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatAppServer {
public static void main(String[] args) {

    try {

        // Defining/ Initialising new ServerSocket

        ServerSocket caServerSocket = new ServerSocket(5000);
        System.out.println("Waiting for Client...");
        Socket nSocket = caServerSocket.accept();
        System.out.println("Client connected.");

        // Setting up InputStream for the ServerSocket to receive the message String

        DataInputStream clientMessages = new DataInputStream(nSocket.getInputStream());
        String s = clientMessages.readLine();

        // Testing if the Input String got received

        System.out.println(s);

        // Setting up OutputStream to send back the String

        DataOutputStream clientMessagesBack = new DataOutputStream(nSocket.getOutputStream());
        clientMessagesBack.writeBytes(s);

        caServerSocket.close();

    } catch (IOException ioE) {
        ioE.printStackTrace();
    }
}
}

标签: javajavafxstreamjava-ioeventhandler

解决方案


推荐阅读