我是套接字和网络编程的新手,这是我尝试创建一个简单的聊天服务器,我的代码似乎没有问题(据我所知),并且可以很好地编译,但是当我尝试运行它时,似乎被卡住,什么也没做。
道歉,如果已经有人问过这样的问题,我什么也没找到,但是我不确定什么是错的,所以我真的不知道要寻找什么。
服务器:
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);
}