开发者

Exception with ObjectInputStream

开发者 https://www.devze.com 2023-04-11 12:23 出处:网络
I have three classes, the client, the server and the handler (which is going to handle the server connections) as I show below:

I have three classes, the client, the server and the handler (which is going to handle the server connections) as I show below:

// The Client
    public void sendSomePackage() {
        try {
            socket = new Socket("localhost", 54321);
            sos = socket.getOutputStream();
            oos = new ObjectOutputStream(sockOutput);
        } catch (IOException e) {
            e.printStackTrace(System.e开发者_如何学编程rr);
            return;
        }

        // About to start reading/writing to/from socket

        try {
            Package package = new Package(100);
            oos.writeObject(pacote);

        } catch (IOException e) {
            e.printStackTrace(System.err);
        }

        try {
            Thread.sleep(50);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Done reading/writing to/from socket, closing socket.

        try {
            sock.close();
        } catch (IOException e) {
            System.err.println("Exception closing socket.");
            e.printStackTrace(System.err);
        }
        //Exiting
    }

Now the server class:

// The Server - with a method that just wait for connections

    public void waitForConnections() {
        while (true) {
            try {
                socket = serverSocket.accept();

                // Server:Accepted new socket, creating new handler for it
                SimpleHandler handler = new SimpleHandler(socket);
                handler.start();

                // Server:Finished with socket, waiting for next connection
            }
            catch (IOException e){
                e.printStackTrace(System.err);
            }
        }
    }

My handler, which just handle the server connections:

@Override
public void run() {
    //Handler: Handler run() starting
    while (true) {
        try {
            package = (Package) ois.readObject();
            if (pacote != null) {
                System.out.println("Package received " + pacote.getSourceid());
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
            break;
        }
    }

    try {
        // SimpleHandler:Closing socket
        sock.close();
        ois.close();
    } catch (Exception e) {
        // Handler: Exception while closing socket, e=" + e);
        e.printStackTrace(System.err);
    }

}

The idea is the client send some 'package' object to my server which is going to keep running receiving the 'package' object any time. The connection works fine, but in the end of the program an exception is launched, this is the one:

Package received 100
java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at br.ufscar.socket.SimpleHandler.run(SimpleHandler.java:45)
    at java.lang.Thread.run(Unknown Source)

I already search for something on Google but nothing so far. Any idea ?


This is working exactly as you want it to (probably). It reads the 100 then goes through the loop again (while(true) never stops looping until a break statement) and throws an exception because no more data has been sent and it goes to the catch statement and prints the error before exiting your while loop.


EOFException ist an IOException that indicates the end of an stream.


Here we say that if there aren't any more bytes to read then we should break out of the while loop before trying to read the object, etc.

while (true) {
   if (ois.read() == -1) break;
   //...rest of the code
}


Ok, this is how object streams work and the solution that works everywhere.

Object stream data is preceded by a 4 byte 'magical' sequence AC ED 00 05. An ObjectInputStream will peek for this data at construction time rather than before the first read. And that's logical: one wants to be sure it is a proper stream before being too far in an application. The sequence is buffered by the ObjectOutputStream at construction time so that it is pushed on the stream at the first write.

This method gives rise to complexities in buffered situations or transferring via sockets. Fortunately there is a just as simple as effective solution to all these problems:

Flush the ObjectOutputStream immediately after construction!

ObjectOutputStream myStream =  new ObjectOutputStream ( anotherStream );
myStream.flush();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号