您的位置:首页 > Web前端

java.io.EOFException

2014-01-19 22:48 351 查看
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2554)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at socket.UserClient1.readObject(UserClient1.java:122)
at socket.UserClient1.clientStart(UserClient1.java:100)
at socket.UserClient1.getUserClient(UserClient1.java:66)
at swing.WindowFactory.getUserClient(WindowFactory.java:32)
at swing.UserWin1.main(UserWin1.java:14)

在server端没有给client端写数据,在server端读取到client端的数据后,及关闭了socket与流,是client的readObject没有读取到流(no byte is available) return -1;

由于val<-1 则 throw new EOFException();

java 源码:

 /**

     * Reads the next byte of data from the input stream. The value byte is

     * returned as an <code>int</code> in the range <code>0</code> to

     * <code>255</code>. If no byte is available because the end of the stream

     * has been reached, the value <code>-1</code> is returned. This method

     * blocks until input data is available, the end of the stream is detected,

     * or an exception is thrown.

     *

     * <p> A subclass must provide an implementation of this method.

     *

     * @return     the next byte of data, or <code>-1</code> if the end of the

     *             stream is reached.

     * @exception  IOException  if an I/O error occurs.

     */

    public abstract int read() throws IOException;

ObjectInputStream 部分源码

/**
* Peeks at (but does not consume) and returns the next byte value in
* the stream, or throws EOFException if end of stream/block data has
* been reached.
*/
byte peekByte() throws IOException {
   int val = peek();
   if (val < 0) {
throw new EOFException();
   }
   return (byte) val;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息