java - 为什么 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();
}
}
}
解决方案
推荐阅读
- virtual-machine - 如何将 VirtualBox 设置为文件的新路径?
- c# - 使用 GroupBy 展平列表
- javascript - 如何创建 Next.js 应用程序的可执行 JAR?
- typescript - 如何为对象文字 npm 模块导出创建打字稿声明?
- android - 如何检查切换按钮组?
- php - Laravel Eloquent - pluck() 角色名称
- eclipse - Spring Tool Suite 无响应
- sql-server - 左连接未返回预期结果
- google-cloud-platform - Pubsub 使用错误的密钥签署 JWT 令牌以进行推送
- zig - 为什么zig中的用户定义类型需要是const?