gpt4 book ai didi

JavaFX 和网络

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:24:12 25 4
gpt4 key购买 nike

我是套接字和网络编程的新手,这是我尝试创建一个简单的聊天服务器,我的代码似乎没有任何问题(据我所知),它编译得很好,但是当我尝试运行它时,它似乎卡住了,什么也没做。

很抱歉,如果已经有人问过这样的问题,我什么也没找到,但我不确定哪里出了问题,所以我真的不知道要寻找什么。

服务器:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

public class ChatServer extends Stage {
TextArea informationArea = new TextArea();
String message = null;

public ChatServer() {
informationArea.setEditable(false);
setTitle("Chat Server");
setScene(new Scene(informationArea, 500, 300));
show();

try {
ServerSocket serverSocket = new ServerSocket(8000);
informationArea.appendText("Server started at " + new Date() + '\n');

int clientNumber = 1;

while(true) {
Socket socket = serverSocket.accept();
InetAddress inetAddress = socket.getInetAddress();

informationArea.appendText(
"Client: " + clientNumber++
+ "\n\thost name: " + inetAddress.getHostName()
+ "\n\tIP address " + inetAddress.getHostAddress() + "\n\n"
);

ClientHandler task = new ClientHandler(socket);
new Thread(task).start();
}
} catch(IOException ex) {
System.err.println(ex);
}
}

class ClientHandler implements Runnable {
private Socket socket;

ClientHandler(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
try {
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());

while (true) {
if (message != null) {
outputToClient.writeUTF(message);
}
message = inputFromClient.readUTF();
}
} catch(IOException e) {
System.err.println(e);
}
}
}
}

运行它的类:

import javafx.application.Application;
import javafx.stage.Stage;

public class RunChatServer extends Application {
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage stage) {
stage = new ChatServer();
}
}

如果您能提供有关问题所在和原因、实现此目标的正确方法以及任何风格建议的意见,我将不胜感激,因为这是我的第一次尝试,我不知道实现此目标的传统方法。

更新:我认为这可能与阻止 UI 线程有关,我添加了 import javafx.concurrent.Task 并像这样重构了我的 ClientHandler 类:

class ClientHandler extends Task {
private Socket socket;

ClientHandler(Socket socket) {
this.socket = socket;
}

@Override
public Void call() {
try {
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());

while (true) {
if (message != null) {
outputToClient.writeUTF(message);
outputToClient.flush();
}
message = inputFromClient.readUTF();
}
} catch(IOException e) {
System.err.println(e);
}
return null;
}
}

但这并没有解决任何问题......

最佳答案

将服务器接受套接字的循环放在它自己的线程中,而不是阻塞构造函数。

    try {
ServerSocket serverSocket = new ServerSocket(8000);
informationArea.appendText("Server started at " + new Date() + '\n');

new Thread(() -> {
int clientNumber = 1;
while (true) {
try {
Socket socket = serverSocket.accept();
InetAddress inetAddress = socket.getInetAddress();

Platform.runLater(() ->
informationArea.appendText(
"Client: " + clientNumber++
+ "\n\thost name: " + inetAddress.getHostName()
+ "\n\tIP address " + inetAddress.getHostAddress() + "\n\n"
)
);

ClientHandler task = new ClientHandler(socket);
new Thread(task).start();
} catch (IOException ex) {
Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}).start();

} catch (IOException ex) {
System.err.println(ex);
}

关于JavaFX 和网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32170954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com