George Phillips
George Phillips

Reputation: 185

Not receiving final data from stream

I am currently not receiving the last object from my object stream until another set of data is sent from the server. The objects sent have either a 1,2 or 3 int to state whether they are the first middle or last packets. I have sent these objects to an array and analysed this in the debugger, it shows that the last packet does not come through until the first one is sent again.

This is the server code:

public void telleveryone(Object message){
    Iterator it = clientOutputStream.iterator();
    while(it.hasNext()){
        try{

            ObjectOutputStream everyone = (ObjectOutputStream)it.next();
            everyone.writeObject(message);
            everyone.flush();

        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

}

This is the receiving code on the client:

public void run() {

    try{
        sock = new Socket("10.42.34.46", 1337);

        InputStream is = sock.getInputStream();

        ois = new ObjectInputStream(new BufferedInputStream(is));

        OutputStream os = sock.getOutputStream();

        oops = new ObjectOutputStream(os);

        while(true){

                serverDraw =   (com.DrawTastic.Drawring) ois.readObject();

                test.add(serverDraw);

        }


    }catch(IOException ex){
        ex.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

Upvotes: 2

Views: 77

Answers (2)

covertCoder
covertCoder

Reputation: 426

I didn't read over the code thoroughly, but I've run into this problem with Python. I noticed a lot of my commands to the server were not executing until my program pinged the server again. My solution was to ensure there was a newline at the end of each command to the server, or you could flush the buffer.

Upvotes: 0

Eng.Fouad
Eng.Fouad

Reputation: 117569

You need to flush the header:

callers may wish to flush the stream immediately to ensure that constructors for receiving ObjectInputStreams will not block when reading the header

oops = new ObjectOutputStream(os);
oops.flush();

Upvotes: 2

Related Questions