2

I'm getting the following exception for the code included below that. This works fine when the while() loop is excluded. Why is this?

Oct 6, 2011 1:19:31 AM com.mytunes.server.ServerHandler run
SEVERE: null
java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2552)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at com.mytunes.server.ServerHandler.run(ServerHandler.java:68)

Class ServerHandler:

public class ServerHandler extends Thread {
 .
 .
 .

public ServerHandler(...){
...
}


public void run(){

    try {

        os = s.getOutputStream();
        oos = new ObjectOutputStream(os);

        is = s.getInputStream();
        ois = new ObjectInputStream(is);

        while(true){

            msg = (Messenger) ois.readObject(); 

            String methodType = msg.getKey();

            //validating various data passed from the serialized object
            if(methodType.equals("validateCard")){

            } else if(methodType.equals("validatePIN")){

            }

        }

   } catch (SQLException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    } finally{
        try {
            ois.close();
            is.close();
            oos.close();
            os.close();
            s.close();
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }



}

}

Class Server

public class Server{

    ...
    ServerSocket ss;
    ...

    public static void main(String args[]){
        Server server = new Server();
       server.init();

    }

    public void init(){
        try {
            System.out.println("Server started...");

            ss = new ServerSocket(port);

             System.out.println("Listening on " + ss.getInetAddress() + ":" + ss.getLocalPort());

            System.out.println("Waiting for clients...");

            while(true){
                  Socket incoming_socket = ss.accept(); // returns a Socket connection object if received
                  new ServerHandler(...).start();
            }

        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


 }

1 Answer 1

5

You keep trying to read objects forever, never breaking out of the loop. When the client closes the connection, the stream will run out of data, and the ObjectInputStream.readObject method will throw the exception you're seeing.

How many objects did you expect to be in the stream, and why are you reading past the end of them?

Sign up to request clarification or add additional context in comments.

6 Comments

+1 for the quick reply! what i want to do is this: each client will initiate new socket connections with the Server. while being inside that connection, i want the client to communicate (two-way) with the Server as I thought connecting-and-disconneting repeatedly is unnecessary. Is it a bad practice? Does the Client always need to close the socket,ois,is,oos,os ?
@coder9: It's generally fine to do that sort of communication, but you'll probably need to know when to stop... it's not clear to me that ObjectInputStream gives you that information easily. (I should warn you that Java binary serialization is generally problematic in various ways - I'd consider something like Protocol Buffers if I were you.)
Maybe I should stick with this for now since this is only for an academic assignment :) thanks.
@Jon:How is it possible to know so much on Java and still not be your main language?(I have seen you are into C#).I am struggling just to be good in 1 language.How do you do this?(Always asking to find ways to improve myself).
@user384706: Spending a fair amount of time here helps - and although I love C#, Java is the one I use professionally. I'm just an enthusiastic amateur at C# :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.