Question

  • Creator
    Topic
  • #2224670

    Problem with Java writing a Unix Named Pipe

    Locked

    by jeremiorama ·

    Hi, I am having a hard time to figure out what is wrong with my Java code.

    I created a named pipe with unix like this:

    /home/users/myself> mkfifo pipe

    Then, in my Java code, I do this:

    ////////////////////////////////////////////
    BufferedWriter lLogPipeWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(“/home/users/myself/pipe”)));
    String lLine = lLogPipeReader.write(“helloWorld”);
    ////////////////////////////////////////////

    Am I doing this right or should i avoid some things? It blocks when it tries to create the FileOutputStream. See this is the part of the stack trace where it blocks:

    ////////////////////////////////////////////
    FileOutputStream.open(String) line: not available [native method]
    FileOutputStream.(File, boolean) line: 179
    FileOutputStream.
    (String) line: 70
    ////////////////////////////////////////////

    The String given (third line) is the right one (/home/users/myself/pipe). Thus, the File given (second line) is correct and the boolean is ‘false’. It tries to open the file with the String ‘/home/users/myself/pipe’ but it blocks there.

    Do you have an idea of what the problem could be?

    Thanks a lot!

    -Jeremie

All Answers

  • Author
    Replies
    • #2617703

      Clarifications

      by jeremiorama ·

      In reply to Problem with Java writing a Unix Named Pipe

      Clarifications

    • #2618190

      Precisions

      by jeremiorama ·

      In reply to Problem with Java writing a Unix Named Pipe

      Hi again,

      I’ve learned that a unix named pipe blocks when a reader and a writer are not both acting on the named pipe.

      Is there a way of checking if there is one first? Is there a way to find out if a reader is actually listening on the named pipe?

      What I try to do is have applications write into this named pipe. However, if no one is listening to read, I don’t want the applications to wait for writing. Is there a way to check whether a reader is actually listening on the named pipe?

      I’m using Java.

      Thanks a lot!

      • #2628409

        Problem with Java writing a Unix Named Pipe

        by moturiuppi ·

        In reply to Precisions

        just use Filewriter() instead of outputstream.this works fine.But i suggest you to use FileChannels instead to access any files.

    • #2626325

      This works for me…

      by mike.curwen ·

      In reply to Problem with Java writing a Unix Named Pipe

      It’s probably really bad coding, I don’t use java.io hardly at all (I’m a web guy).

      import java.io.*;

      public class TestPipe {
      public static void main(String[] args) throws IOException, InterruptedException {
      TestPipe app = new TestPipe();
      }

      public TestPipe() throws IOException, InterruptedException {

      System.out.println(“open reader”);
      new ReadPipe();

      System.out.println(“open writer”);
      new WritePipe();

      Thread.sleep(2000);

      }

      class ReadPipe extends Thread {

      public ReadPipe() {
      this.setName(“Pipe Reader”);
      this.setDaemon(true);
      this.start();
      }

      public void run() {
      try {
      BufferedReader in = new BufferedReader(new FileReader(“/home/mikec/pipe”));

      while (!in.ready());
      System.out.println(in.readLine());

      } catch (Exception e) {
      System.err.println(e);
      }
      }

      }

      class WritePipe extends Thread {

      public WritePipe() {
      this.setName(“Pipe Writer”);
      this.setDaemon(true);
      this.start();
      }

      public void run() {
      try {
      OutputStreamWriter out = new OutputStreamWriter(
      new FileOutputStream(“/home/mikec/pipe”)
      );
      System.out.println(“write something”);
      out.write(“HELLO, WORLD\n”);
      out.flush();
      out.close();

      } catch (Exception e) {
      System.err.println(e);
      }

      }
      }
      }

    • #3014701

      did you try this?

      by anlin ·

      In reply to Problem with Java writing a Unix Named Pipe

      I blocks until there is a reader on the FIFO according to POSIX specification.
      Try open it in read mode too using script or FileInputStream.

Viewing 3 reply threads