gpt4 book ai didi

Java 套接字 writeObject 卡住

转载 作者:太空宇宙 更新时间:2023-11-04 13:48:43 25 4
gpt4 key购买 nike

我正在尝试创建一些本地聊天软件。一切正常,但只停留在一点上;当用户单击按钮时,我的程序会卡住。

我编写了一个修改后的类,它的行为适用于服务器和客户端。

下面是我的代码:

package connections;

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import ui.ChatWindow;

public class Chat{

private SocketChannel socketChannel;
private ConnectionType connectionType;

private ObjectOutputStream out;
private ObjectInputStream in;

private String strClientName;

private ChatWindow chatWindow;
private String strMessage;

public Chat(SocketChannel socketChannel, ConnectionType connectionType) {
this.socketChannel = socketChannel;
this.connectionType = connectionType;

init();
}

private void init() {

new Thread(this::initThread).start();
}

private void initThread() {
try {
out = new ObjectOutputStream(socketChannel.socket().getOutputStream());
in = new ObjectInputStream(socketChannel.socket().getInputStream());

if (connectionType == ConnectionType.SERVER) {
getClientName();
sendMyName();
} else {
sendMyName();
getClientName();
}

chatWindow = new ChatWindow();
chatWindow.btnSend.addActionListener(this::eventBtnSend);
chatWindow.setTitle(strClientName);
chatWindow.setSize(600, 400);
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

if (connectionType == ConnectionType.CLIENT) {
chatWindow.setVisible(true);
chatWindow.requestFocus();
}

System.out.println("here");

beginChat();

System.out.println("thread ends");

} catch (IOException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void getClientName() {

try {
strClientName = (String) in.readObject();
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(Status.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void sendMyName() {

try {
out.writeObject(main.Config.myName);
out.flush();
} catch (IOException ex) {
Logger.getLogger(Status.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void beginChat() {
while (true) {
try {
waitForMessage();
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

private void waitForMessage() throws IOException, ClassNotFoundException {
System.out.println("waiting for Message : " + connectionType);
strMessage = (String) in.readObject();
System.out.println("Message received");
chatWindow.txtDisplay.append(strClientName + " : " + strMessage + "\n");

if(chatWindow.isVisible() == false){
chatWindow.setVisible(true);
}
}

private void sendMessage(String strMessage) {

try {
out.writeObject(strMessage);
out.flush();
} catch (IOException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void eventBtnSend(ActionEvent ae){
System.out.println("a");
sendMessage(chatWindow.txtMessage.getText());
System.out.println("b");
chatWindow.txtDisplay.append("Me : " + chatWindow.txtMessage.getText() + "\n");
System.out.println("c");
chatWindow.txtMessage.setText("");
}
}

当服务器和客户端连接时,我可以看到以下输出:

here
waiting for Message : SERVER
here
waiting for Message : CLIENT

但是当我单击发送按钮时,我的程序卡住了。单击按钮后我可以看到:

a on console.

最佳答案

这个词是“ block ”。它会阻塞 I/O。它会阻塞。

不要在事件线程上执行阻塞操作。所有网络代码都应该在单独的线程上运行。其中包括 readObject() 和 writeObject()、flush() 以及创建流。

关于Java 套接字 writeObject 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30550735/

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