在我的应用程序中,为什么readInt()总是抛出EOFException? [英] In my application, why does readInt() always throw an EOFException?

查看:298
本文介绍了在我的应用程序中,为什么readInt()总是抛出EOFException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(请原谅我,因为我不经常用Java编写。)

(Forgive me because I do not write in Java very often.)

我正在用Java编写客户端网络应用程序,有趣的问题。每次对readInt()的调用都会引发 EOFException 。变量的类型为 DataInputStream (初始化为: DataInputStream din = new DataInputStream(new BufferedInputStream(sock.getInputStream())); 其中袜子的类型为 Socket )。

I'm writing a client-side network application in Java and I'm having an interesting issue. Every call to readInt() throws an EOFException. The variable is of type DataInputStream (initialized as: DataInputStream din = new DataInputStream(new BufferedInputStream(sock.getInputStream())); where sock is of type Socket).

现在, sock.isInputShutdown()返回 false socket.isConnected()返回 true 。我假设这意味着我已经与要连接的另一台计算机建立了有效的连接。我还执行了其他检查,以确保我已正确连接到另一台计算机。

Now, sock.isInputShutdown() returns false and socket.isConnected() returns true. I'm assuming that this means that I have a valid connection to the other machine I'm connecting to. I've also performed other checks to ensure that I'm properly connected to the other machine.

DataInputStream 设置不正确?我有错过任何先决条件吗?

Is it possible that the DataInputStream was not set up correctly? Are there any preconditions that I have missed?

任何帮助都将不胜感激。

Any help is greatly appreciated.

@tofubeer:我实际上向套接字写入了17个字节。套接字已连接到另一台计算机,我正在等待来自该计算机的输入(如果不清楚,抱歉)。我首先成功地从流中读取了信息(发起了握手),并且一切正常。我现在正在检查发送的请求是否格式错误,但我认为不是。另外,我尝试从流中读取单个字节(通过 read()),并返回-1。

@tofubeer: I actually wrote 17 bytes to the socket. The socket is connected to another machine and I'm waiting on input from that machine (I'm sorry if this was unclear). I successfully read from the stream (to initiate a handshake) first and this worked just fine. I'm checking now to see if my sent-requests are malformed, but I don't think they are. Also, I tried reading a single byte from the stream (via read()) and it returned -1.

推荐答案

要检查的一些事情:


  • 握手是否消耗了超过13个字节,而少于

  • 是否要通过DataOutputStream.writeInt()写入要读取的整数?

  • 是否从中刷新流?

编辑:我看了一下Java源代码(我有1.4个源代码我的桌面,不确定使用的是哪个版本),则问题可能出在BufferedInputStream中。 DataInputStream.readInt()仅调用BufferedInputStream.read()四次。如果缓冲区的缓冲区已用完(例如,如果其第一次读取仅获得16个字节),则BufferedInputStream.read()会调用BufferedInputStream.fill()。 BufferedInputStream.fill()调用基础InputStream的read(byte [],int,int)方法,根据约定,该方法实际上可能不会读取任何内容!如果发生这种情况,则BufferedInputStream.read()将返回错误的EOF。

Edit: I took a look at the Java sources (I have the 1.4 sources on my desktop, not sure which version you're using) and the problem might be in BufferedInputStream. DataInputStream.readInt() is just calling BufferedInputStream.read() four times. BufferedInputStream.read() is calling BufferedInputStream.fill() if its buffer is exhausted (e.g., if its first read only got 16 bytes). BufferedInputStream.fill() calls the underlying InputStream's read(byte[], int, int) method, which by contract might not actually read anything! If this happens, BufferedInputStream.read() will return an erroneous EOF.

所有这些都假设我正确地读取了所有这些内容,而事实并非如此。我只是快速浏览了一下源代码。

This is all assuming that I'm reading all of this correctly, which might not be the case. I only took a quick peek at the sources.

我怀疑您的BufferedInputStream在第一次读取时仅获取流的前16个字节。我很好奇,您的DataInputStream的available()在readInt之前返回。如果您还没有这样做,建议您在写入无法读取的int之后刷新OutputStream。

I suspect that your BufferedInputStream is only getting the first 16 bytes of the stream in its first read. I'd be curious what your DataInputStream's available() returns right before the readInt. If you're not already, I'd suggest you flush your OutputStream after writing the int you can't read as a possible workaround.

这篇关于在我的应用程序中,为什么readInt()总是抛出EOFException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆