gpt4 book ai didi

java - java中使用socket读写时为什么电脑会挂起

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

我在这里读过类似的帖子,但不明白什么是解决方案。

我有一个为每个客户端创建新线程的服务器。主题是:

private static class Capitalizer extends Thread {
private Socket socket;
private int clientNumber;

public Capitalizer(Socket socket, int clientNumber) {
this.socket = socket;
this.clientNumber = clientNumber;
log("New connection with client " + clientNumber + " at " + socket);
}

private void log(String message) {
System.out.println(message);
}

@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

out.println("Hello, you are a client " + clientNumber);
out.println("Enter a line with only a period to exit");

while (true) {
String line = in.readLine();
if (line == null || line.equals("."))
break;
out.println(line.toUpperCase());
System.out.println("Server capitalized " + line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
log("Connection with client " + clientNumber + " is closed");
}
}
}

客户是:

public class CapitalizeClient {
private BufferedReader in;
private PrintWriter out;
private JFrame mainWindow = new JFrame("Capitalize Client");
private JTextField dataField = new JTextField(40);
private JTextArea messageArea = new JTextArea(8, 60);

public CapitalizeClient() {

messageArea.setEditable(false);
mainWindow.getContentPane().add(dataField, BorderLayout.NORTH);
mainWindow.getContentPane().add(new JScrollPane(messageArea));

dataField.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
String text = dataField.getText();
out.println(text);
System.out.println("text is sent to server");
try {
String upperCased = in.readLine();
if (upperCased == null || upperCased.equals("."))
System.exit(0);
System.out.println("ActionListener " + text);
messageArea.append(upperCased + "\n");
dataField.selectAll();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}

private void connectToServer() throws UnknownHostException, IOException {
String serverAddress = JOptionPane.showInputDialog(mainWindow, "Enter word 'localhost'",
"Welcome to the capitalization program", JOptionPane.QUESTION_MESSAGE);

Socket socket = new Socket(serverAddress, CapitalizeServer.port);

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);

String line;
while ((line = in.readLine()) != null) {
messageArea.append(line + "\n");
}

}

public static void main(String[] args) throws UnknownHostException, IOException {
CapitalizeClient client = new CapitalizeClient();
client.mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.mainWindow.pack();
client.mainWindow.setVisible(true);
client.connectToServer();
}
}

Capitalizer 线程运行正常,但 dataField 的 ActionListener 运行,直到记录消息文本已发送到服务器

程序挂断后:

String upperCased = in.readLine();

dataFieldActionListener中。

这是为什么呢?谢谢!

最佳答案

您需要刷新服务器中的OutputStream

out.println(line.toUpperCase());
out.flush();

关于java - java中使用socket读写时为什么电脑会挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39729855/

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