无法实例化 ObjectInputStream 以读取用户的输入

can't instantiate ObjectInputStream to read input from user

我无法添加 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 objects 毫无意义。用户不可能在控制台中输入有效的序列化 Java 对象。不过,He/She 可以输入文字。所以使用 Reader 或扫描仪。