-2

I have 3 very small classes.

The main class:

import java.io.*;

public class ConnectionManager {
    public static void main(String argv[]) {

        try {
            PipedOutputStream pout = new PipedOutputStream();
            PipedInputStream pin = new PipedInputStream(pout);

            Sender s = new Sender(pout, true);
            Receiver r = new Receiver(pin, true);
            System.out.println("Starting threads");
            s.start();
            r.start();
        } catch (Exception e) {}
    }
}

The Sender class

import java.io.*;
import java.util.Random;

public class Sender extends Thread {
    ObjectOutputStream oos;
    boolean primitive;

    public Sender(OutputStream os, boolean primitive) {
        try {
            oos = new ObjectOutputStream(os);
        } catch (Exception e) {}
        this.primitive = primitive;
    }

    public void run() {
        Random rand = new Random();
        while (true) {
            try {
                System.out.println("Integer is being sent");
                oos.writeInt(10);
                Thread.sleep(1000);
            } catch (Exception e) {}
        }
    }
}

And the Receiver class

import java.io.*;

public class Receiver extends Thread {
    ObjectInputStream ois;
    boolean primitive;

    public Receiver(InputStream is, boolean primitive) {
        try {
            ois = new ObjectInputStream(is);
        } catch (Exception e) {}
        this.primitive = primitive;
    }

    public void run() {
        System.out.println("Receiver is starting");
        while (true) {
            try {
                int x = ois.readInt();
                System.out.print("An int was read: " + x);
            } catch (Exception e) {}
        }
    }
}

Please ignore seemingly unused variables like primitive and rand. They're holdovers from slightly different versions that I was testing out earlier and I was too lazy to remove them.

Anyway, when I run the main method in ConnectionManager, I get this as output:

Starting threads
Receiver is starting
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
//... ad infinitum 

Why is the receiver thread not getting the messages that are piped through? What am I missing here?

Manuel
  • 2,143
  • 5
  • 20
  • 22
  • 1
    Everywhere you have catch (Exception e) {}, replace it with catch (Exception e) { e.printStackTrace(); } – opensam Apr 26 '17 at 16:32
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Apr 26 '17 at 16:36

1 Answers1

0

In your code, there is an exception like java.io.IOException: Read end dead.

You are NOT able to spot because you are suppressing them with empty catch blocks.

The main point is that you need to change Sender and Receiver classes to use PipedOutputStream and PipedInputStream as shown below :

Sender class:

public class Sender extends Thread {
    PipedOutputStream oos;

    public Sender(PipedOutputStream os) {
        try {
            this.oos = os;
        } catch (Exception e) {System.out.println(e);}
    }

    public void run() {
        try {
                System.out.println("Integer is being sent");
                oos.write(10);
                oos.close();
                Thread.sleep(1000);
            } catch (Exception e) {System.out.println(e);}
        }
}

Receiver class:

public class Receiver extends Thread {
    PipedInputStream ois;
    public Receiver(PipedInputStream is) {
        try {
            this.ois = is;
        } catch (Exception e) {System.out.println(e);}
    }

    public void run() {
        System.out.println("Receiver is starting");
          try {
                int x = ois.read();
                System.out.print("An int was read: " + x);
            } catch (Exception e) {System.out.println(e);}
    }
}

main() method:

public static void main(String[] args) throws Exception {
         try {
                PipedOutputStream pout = new PipedOutputStream();
                PipedInputStream pin = new PipedInputStream(pout);

                Sender s = new Sender(pout);
                Receiver r = new Receiver(pin);
                System.out.println("Starting threads");
                s.start();
                r.start();
            } catch (Exception e) {
                System.out.println(e);
            }
     }

OUTPUT:

Starting threads
Receiver is starting
Integer is being sent
An int was read: 10

As a side note, remember that, empty catch blocks are very bad practice as they hide the exceptions, so I strongly suggest not to use them in the code.

Vasu
  • 21,832
  • 11
  • 51
  • 67
  • Hi, thank you for your answer but one of my goals is to be able to send specific data types rather than just int. I didn't think I could, say, send a double or a boolean using PipedOutputStream. ObjectOutputStream supports methods like writeInt(), writeDouble(), writeBoolean(), etc.. edit: thanks for the tip on the empty catch blocks. – Manuel Apr 26 '17 at 16:41