gpt4 book ai didi

java - 在 Socket.getInputStream 之前向 SocketInputStream 发送一些内容?

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

我刚才遇到了一个奇怪的错误。我正在研究可序列化的对象,我使用了两个对象,通过套接字连接相互通信。这两个应用程序是相同的(Peer 2 Peer),但有两个实例正在运行并尝试通信。

问题似乎围绕着获得ObjectInputStream从 socket InputStream 。我的错误发生在 reader = new ObjectInputSream(cliIn) ,我被抛出了 SocketTimeoutException (这本身很奇怪,我本以为是在 read() 而不是装饰我的流时。下面是我尝试做的事情。

Socket clientSocket;
clientSocket = new Socket(InetAddress.getByName(aHostIP), aPort);
clientSocket.setSoTimeout(3000);

InputStream cliIn = clientSocket.getInputStream();
ObjectInputStream reader = null;
reader = new ObjectInputStream(cliIn); // <--- Error here
ObjectOutputStream writer;
writer = new ObjectOutputStream(new BufferedOutputStream(
clientSocket.getOutputStream()
));

writer.writeObject(new Request(aPort));
writer.flush();

奇怪的是,如果我移动 ObjectInputStream 的创建直到我写信给OutputStream之后(此时另一个对象将响应该请求),然后就不再出现错误。

Socket clientSocket;
clientSocket = new Socket(InetAddress.getByName(aHostIP), aPort);
clientSocket.setSoTimeout(3000);

ObjectOutputStream writer;
writer = new ObjectOutputStream(new BufferedOutputStream(
clientSocket.getOutputStream()
));

writer.writeObject(new Request(aPort));
writer.flush();

InputStream cliIn = clientSocket.getInputStream();
ObjectInputStream reader = null;
reader = new ObjectInputStream(cliIn); // <--- No more error

另一方(响应方)如下所示:

ObjectInputStream in = new ObjectInputStream(aConnection.getInputStream());
Object req = in.readObject();
SysIO.print("FROM OBJECT! : " + req.toString());
SysIO.print("FROM OBJECT! Port: " + ((Request) req).getPort());

Request res = new Request(15);

ObjectOutputStream out = new ObjectOutputStream(
new BufferedOutputStream(aConnection.getOutputStream()));
out.writeObject(res);
out.flush();

它现在可以工作了,但我很想知道问题是什么以供将来引用。我可以看到,这可能很难准确地说,但也许您有一些进一步调查的指示,并发问题,我使用的套接字或缓冲区是否错误?

(我省略了一些错误处理以使其更清晰。)

最佳答案

答案就在 ObjectInputStream documentation :

public ObjectInputStream(InputStream in)
throws IOException

Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

If a security manager is installed, this constructor will check for the "enableSubclassImplementation" SerializablePermission when invoked directly or indirectly by the constructor of a subclass which overrides the ObjectInputStream.readFields or ObjectInputStream.readUnshared methods.

Parameters:

in - input stream to read from

Throws:

StreamCorruptedException - if the stream header is incorrect

IOException - if an I/O error occurs while reading stream header

SecurityException - if untrusted subclass illegally overrides security-sensitive methods

NullPointerException - if in is null

在构造过程中,流尝试从套接字读取序列化 header ,但套接字读取超时,因为对方尚未发送任何序列化对象。

关于java - 在 Socket.getInputStream 之前向 SocketInputStream 发送一些内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40622241/

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