How to open a Windows named pipe from Java?
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();
}
An efficient way to use Windows Named Pipe for IPC
Seems like you're constantly polling process states stdin on server side without waiting for any events or sleeping the thread.
Probably you want to take a look here:
Concurrent read/write of named pipe in Java (on windows)
Cheers!
Read continuously from a named pipe using Java
- What happens if there is no data from pipe for some time ?
The program blocks until there is data to read or until EOF is detected, just like a Reader
connected to any other kind of file.
- Can Reader object be reused when EOF is encountered ?
I wouldn't count on it. It would be safer to close the Reader
and create a new one in that case.
- Is EOF is sent by pipe only when it terminate and not otherwise ?
On Unix, EOF will be received from the pipe after it goes from having one writer to having zero, when no more data are available from it. I am uncertain whether Windows named pipe semantics differ in this regard, but since you're on Linux, that doesn't matter to you.
- Does pipe guarantees to be alive and working unless something really goes wrong ?
If the named pipe in fact exists on the file system and you have sufficient permission, then you should reliably be able to open it for reading, but that may block until there is at least one writer. Other than that, I'm not sure what you mean.
Named pipe is connected while checking if it exists from Java
The reason this problem occurs is that File.exists()
on Windows is implemented using a sequence of native function calls to CreateFile
, GetFileInformationByHandle
and CloseHandle
. See the getFileInformation
function in the Java source code. From a named pipe perspective this is bad because on Windows named pipes have to be reset between uses and the CreateFile
call in that native function counts as a use.
The solution is to ask forgiveness rather than permission when opening the named pipe on the Java side. Something along the lines of:
File file = new File("\\\\.\\pipe\\Pipe");
while (true) {
try {
return new FileInputStream(file);
} catch (IOException e) {
Thread.sleep(20);
}
}
(Obviously you might not want to loop forever in practice, but the code in the question did.)
Can't open Windows Named Pipe for writing?
the 4-th parameter of CreateNamedPipe
- nMaxInstances
- this is The maximum number of instances that can be created for this pipe.
so this is not instance count created in single call, but maximum count which can be created. single call to CreateNamedPipe
always create one (1) instance of pipe. if you want have 4 instance - you need call CreateNamedPipe
4 time. also initially pipe created in listening state, so client can just connect to it by call CreateFile
. but after connection is broken (because client close self handle) and you want accept new client connections for the same pipe instance - you need call DisconnectNamedPipe
and then ConnectNamedPipe
- only after this new client can again connect to the same pipe instance.
but anyway, even if you create only single pipe instance, by single call CreateNamedPipeA
- first client can connect to it. error 231 - i guess that real source of error is STATUS_PIPE_NOT_AVAILABLE
(An instance of a named pipe cannot be
found in the listening state) - you can check this by call RtlGetLastNtStatus()
after CreateFile
fail instead GetLastError()
say that pipe name is valid, no problems with access, but somebody already connect (may be already disconnect) to pipe - never first call to CreateFile
return this error
Related Topics
Java - Convert Image to Base64
What Are the Pros and Cons of Performing Calculations in SQL VS. in Your Application
Filtering Database Rows with Spring-Data-JPA and Spring-Mvc
Implement Converters for Entities with Java Generics
Byte Array and Int Conversion in Java
Java Inputstream Blocking Read
Why Are Getter and Setter Method Important in Java
Getting Class Type from String
Missing Return Statement in a Non-Void Method Compiles
How to Make a Color Transparent in a Bufferedimage and Save as Png
What Does Biginteger Having No Limit Mean
Reserved Words as Names or Identifiers