This simple "Hello Word"-example illustrate communication between Java and .Net process. Named pipe is used as communication channel. Application roles are following .Net is server daemon and Java is client.

.Net: Echo daemon

while (true)
{
//Create pipe instance
NamedPipeServerStream pipeServer =
new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4);
Console.WriteLine("[ECHO DAEMON] NamedPipeServerStream thread created.");

//wait for connection
Console.WriteLine("[ECHO DAEMON] Wait for a client to connect");
pipeServer.WaitForConnection();

Console.WriteLine("[ECHO DAEMON] Client connected.");
try
{
// Stream for the request.
StreamReader sr = new StreamReader(pipeServer);
// Stream for the response.
StreamWriter sw = new StreamWriter(pipeServer);
sw.AutoFlush = true;

// Read request from the stream.
string echo = sr.ReadLine();

Console.WriteLine("[ECHO DAEMON] Request message: " + echo);

// Write response to the stream.
sw.WriteLine("[ECHO]: " + echo);

pipeServer.Disconnect();
}
catch (IOException e)
{
Console.WriteLine("[ECHO DAEMON]ERROR: {0}", e.Message);
}
pipeServer.Close();
}


Java: Echo client

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

Refrences
How to: Use Named Pipes to Communicate Between Processes over a Network
RandomAccessFile
0

Lisää kommentti

ssh-keygen -t rsa -b 4096 -m PEM -f test  

0

Lisää kommentti

Info about me
Info about me
Blogiarkisto
Ladataan