0

I have an ObjectOutputStream and an ObjectInputStream. I try to send ints and objects through them. Now I manage to send through and read up to a point and I don't know why it stops there.

Here is the point:

Reader:

    while (true) {
        start = in.readInt();
        System.out.println("PART 1");
        int temp1 = in.readInt();
        int temp2 = in.readInt();
        int temp3 = in.readInt();
        System.out.println("PART12");
        Chunk temp = new Chunk(temp1,temp2, temp3);
        while (true) {

It doesn't get to part12 (doesn't pass the first int...)

Writer:

 if (chunkList != null) {
        for (Chunk c: chunkList) {
            out.writeInt(-1);
            out.writeInt(c.getLocation().getX());
            out.writeInt(c.getLocation().getY());
            out.writeInt(c.getLocation().getZ());
            if (c.getTileList() != null) {

it passes all of it successfully.

I am every 2ms out.flushing in a separate thread.

Thread:

 while (true)
        {
            while (c.sendPacket()) {

            try
            {
                if (c.getOut() != null)
                {
                    c.getOut().flush();
                }
            }
            catch (IOException ioexception)
            {
                ioexception.printStackTrace();
            }

            try
            {
                sleep(2L);
            }
            catch (InterruptedException interruptedexception) { }
            }
        }

Why does it stop reading at the part with the 3 ints?

Behe
  • 7,572
  • 3
  • 33
  • 46
Trixmix
  • 81
  • 9
  • 1
    (a) Any reason for the spurious flushing thread? (b) What Actual Exception are you getting? And as Lajos says, (c) how are your streams being constructed? – Neil Coffey May 25 '12 at 23:47
  • How many threads are running? Are you experimenting?! Anyway, I suspect one of the thread is running on infinite loop, considering I see a couple while(true) loops. One reason code in the Reader thread does not past the System.out code is that the thread releases control to another thread upon console outputs, i.e. switch thread context. And...I don't know how much time is spent on the Writer thread. You may have to show more code so that we all can see the problem better. – The Original Android May 26 '12 at 00:24
  • a) no but i fixed it to be only when a packet is being sent. b) no exception the program just hangs at the first in line after part 1 c) out = new ObjectOutputStream(client.getSock().getOutputStream()); – Trixmix May 26 '12 at 05:53

1 Answers1

0

I have a feeling that this is a thread-safety issue. As a general rule, streams are not designed to be thread-safe. So, unless the you are synchronizing the two threads at a higher level, one thread writing to a stream and a second thread calling flush is unsafe.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216