My code is in Java and I need to integrate with another program on the same Windows machine that is written in .NET/WPF. The .NET/WPF program is sending data using Windows Named Pipes that are triggered based on certain events that happen in the .NET application. So I just need to setup a listener to read that data from the Windows Named Pipe when it's triggered. Note: I don't need to create the Named Pipe from Java, only to setup a listener to read the Named Pipe coming from the Windows .NET application.
Tried this thread Concurrent read/write of named pipe in Java (on windows) but get this error:
java.io.FileNotFoundException: \.\pipe\pixelcade (The system cannot find the file specified)
This error happens after I've started the .NET program which is generating the Windows Named Pipe. But note that the named pipe is fired upon certain events and not streaming continuously.
try {
// Connect to the pipe
RandomAccessFile pipe = new RandomAccessFile("\\\\.\\pipe\\pixelcade",
"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();
}
The expected result would be a few strings from the named pipes from the .NET application.
This code in C#/WPF is what is needed, just don't know how to code the equivalent in Java.
// 1. Create a Pipe event handler
_pipeServer = new PipeServer();
_pipeServer.PipeMessage += new DelegateMessage(PipesMessageHandler);
// 2. Then listen for a message
private void Listen()
{
try
{
if (userPrefs.NamedPipeID == string.Empty)
_pipeServer.Listen(_marqueeConfigfileName); // _marqueeConfigfileName is
the pipename (shake hand between 2 apps; client and server)
else
_pipeServer.Listen(userPrefs.NamedPipeID); // _marqueeConfigfileName is the
pipename (shake hand between 2 apps; client and server)
}
catch (Exception ex)
{
ApplicationLog.EventWriteLog(ex.Message);
}
}
// 3. Handler, my custom logic goes here
private void PipesMessageHandler(string message)
{
try
{
string[] args = CommandLineToArgs(message);
int paramLengh = args.Length;
if (!CheckAccess())
{
Dispatcher.Invoke(() => PipesMessageHandler(message));
}
else
{
if (paramLengh > 2) _game = args[2];
if (paramLengh > 3) _gameSystem = args[3];
if (paramLengh > 4) _freetext = args[4];
if (paramLengh > 5) _event = args[5];
ApplicationLog.EventWriteLog("Pipe message received Wheel: ["+_gameSystem
+"] item: ["+_game+"]");
if (applySettingsThread != null) applySettingsThread.Abort();
applySettingsThread = this.Dispatcher.BeginInvoke(DispatcherPriority.Send,
new Action(ApplySettings));
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}