gpt4 book ai didi

java - 无法实例化 ObjectInputStream 来读取用户的输入

转载 作者:行者123 更新时间:2023-12-02 13:30:47 25 4
gpt4 key购买 nike

我无法添加ObjectInputStream来读取用户的输入,它总是在此时阻塞。如果我删除服务器中应该读取用户输入然后发送硬编码字符串的 ObjectInputStream ,则此代码可以正常工作。幕后发生了什么?我知道,当创建 ObjectOutputStream 时,它会发送一个 header ,当创建 ObjectInputStream 时,它会读取该 header 。在尝试实例化 oOISUser 之前,我是否需要刷新 System 中的某些内容?

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
public Server() {
ServerSocket oSS = null;
Socket oS = null;
ObjectOutputStream oOOS = null; // to write to socket
ObjectInputStream oOIS = null; // to read from socket
ObjectInputStream oOISUser = null; // to read input from user

try {
oSS = new ServerSocket(1025);
oS = oSS.accept();
oOOS = new ObjectOutputStream(oS.getOutputStream());
oOIS = new ObjectInputStream(oS.getInputStream());
oOISUser = new ObjectInputStream(System.in);`// doesn't get past this

String sToSend = (String) oOISUser.readObject();
System.out.println("server says: " + sToSend);
oOOS.writeObject(sToSend);
oOOS.flush();
System.out.println("server receives: " + (String) oOIS.readObject());
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} finally {
try {
if (oSS != null) oSS.close();
if (oS != null) oS.close();
if (oOOS != null) oOOS.close();
if (oOIS != null) oOIS.close();
if (oOISUser != null) oOISUser.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

public static void main(String[] args) {
Server s = new Server();
}
}

这是客户端的代码:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Client {
public Client() {
Socket oS = null;
ObjectOutputStream oOOS = null;
ObjectInputStream oOIS = null;

try {
oS = new Socket("127.0.0.1", 1025);
oOOS = new ObjectOutputStream(oS.getOutputStream());
oOIS = new ObjectInputStream(oS.getInputStream());

System.out.println("client receives: " + (String) oOIS.readObject());
String sToSend = "hello from client";
System.out.println("client says: " + sToSend);
oOOS.writeObject(sToSend);
oOOS.flush();

} catch (IOException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} finally {
try {
if (oS != null) oS.close();
if (oOOS != null) oOOS.close();
if (oOIS != null) oOIS.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) {
Client c = new Client();
}

}

最佳答案

new ObjectInputStream(System.in)

您自己在问题中说过:

when a ObjectInputStream is created it reads that header

因此,您实际上正在等待用户在控制台中输入 ObjectInputStream header 。发生这种情况的可能性非常小(除非文件通过管道传输到 System.in)。从 System.in 读取序列化的 Java 对象没有什么意义。用户不可能在控制台中键入有效的序列化 Java 对象。不过,他/她可以输入文本。因此,请使用阅读器或扫描仪。

关于java - 无法实例化 ObjectInputStream 来读取用户的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43192002/

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