Question

Locked

Problem with Java writing a Unix Named Pipe

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.<init>(File, boolean) line: 179
FileOutputStream.<init>(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

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Precisions

by jeremiorama In reply to Problem with Java writing ...

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!

Collapse -

Problem with Java writing a Unix Named Pipe

by moturiuppi In reply to Precisions

just use Filewriter(<your path&gt instead of outputstream.this works fine.But i suggest you to use FileChannels instead to access any files.

Collapse -

This works for me...

by mike.curwen In reply to Problem with Java writing ...

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);
}

}
}
}

Collapse -

did you try this?

by anlin In reply to Problem with Java writing ...

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.

Back to Software Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums