Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a Windows named pipe from Java?

On our Linux system we use named pipes for interprocess communication (a producer and a consumer).

In order to test the consumer (Java) code, I would like to implement (in Java) a dummy producer which writes to a named pipe which is connected to the consumer.

Now the test should also work in the Windows development environment. Thus I would like to know how to create a named pipe in Windows from Java. In Linux I can use mkfifo (called using Runtime.exec() ), but how should I do this on Windows?

like image 361
Philipp Avatar asked Mar 11 '09 13:03

Philipp


People also ask

Is a named pipe a file?

A FIFO, also known as a named pipe, is a special file similar to a pipe but with a name on the filesystem. Multiple processes can access this special file for reading and writing like any ordinary file. Thus, the name works only as a reference point for processes that need to use a name in the filesystem.

Where is named pipe file?

Every pipe is placed in the root directory of the named pipe filesystem (NPFS), mounted under the special path \\. \pipe\ (that is, a pipe named "foo" would have a full path name of \\. \pipe\foo ). Anonymous pipes used in pipelining are actually named pipes with a random name.

What are named pipes used for?

Named pipes can be used to provide communication between processes on the same computer or between processes on different computers across a network. If the server service is running, all named pipes are accessible remotely.

Do named pipes store data?

For one thing, named pipe content resides in memory rather than being written to disk. It is passed only when both ends of the pipe have been opened. And you can write to a pipe multiple times before it is opened at the other end and read.


2 Answers

Use Named Pipes to Communicate Between Java and .Net Processes

Relevant part in the link

try {   // Connect to the pipe   RandomAccessFile pipe = new RandomAccessFile("\\\\.\\pipe\\testpipe", "rw");   String echoText = "Hello word\n";   // write to pipe   pipe.write ( echoText.getBytes() );   // read response   String echoResponse = pipe.readLine();   System.out.println("Response: " + echoResponse );   pipe.close(); } catch (Exception e) {   // TODO Auto-generated catch block   e.printStackTrace(); } 
like image 77
v01ver Avatar answered Sep 17 '22 21:09

v01ver


In windows, named pipes exist but they cannot be created as files in a writeable filesystem and there is no command line tool. They live in a special filesystem and can be created only by using the Win32 API.

Looks like you'll have to resort to native code, or switch from pipes to sockets for IPC - probably the best longterm solution, since it's much more portable.

like image 29
Michael Borgwardt Avatar answered Sep 17 '22 21:09

Michael Borgwardt