I am working on an inter-process communication between a .net (v4.5.2) and Javascript node.js (v8.9.0) application. I want to use Windows named pipes for this (and only named pipes). For the Javascript application, I am using the named-pipes package (v0.0.1) I am able to establish a connection between the two applications which tells me that I am not completely off base here. I would expect to see the data event being triggered in the JavaScript application whenever I am writing a string to the NamedPipeServerStream but no data is received. I can not receive any data. Here is the code for .net and the JavaScript application. Any ideas why the data event is not being triggered?
.Net Code
using System;
using System.IO;
using System.IO.Pipes;
namespace NamedPipes
{
class Program
{
static void Main(string[] args)
{
var server = new NamedPipeServerStream("XYZNamedPipe");
Console.WriteLine("Waiting for Connection");
server.WaitForConnection();
Console.WriteLine("Connection Established");
StreamWriter writer = new StreamWriter(server);
int cnt = 0;
while (true)
{
string line = Console.ReadLine();
writer.WriteLine(++cnt.ToString() + ": " + line);
}
}
}
}
JavaScript Code
var NamedPipes = require("named-pipes");
pipe = NamedPipes.connect('XYZNamedPipe')
pipe.on('connect', (str) => {
console.log('connection established');
});
pipe.on('data', (str) => {
console.log('data received');
console.log(str);
});
pipe.on('end', (str) => {
console.log('end');
});
A named pipe is a named, one-way or duplex pipe for communication between the pipe server and one or more pipe clients. All instances of a named pipe share the same pipe name, but each instance has its own buffers and handles, and provides a separate conduit for client/server communication.
Named pipes provide interprocess communication between a pipe server and one or more pipe clients. They offer more functionality than anonymous pipes, which provide interprocess communication on a local computer.
'node-ipc' is a Node. js module for local and remote Inter Process Communication with full support for Linux, Mac and Windows. It also supports all forms of socket communication from low level unix and windows sockets to UDP and secure TLS and TCP sockets.
net.createServer([options][, connectionListener]) Creates a new TCP server. The connectionListener argument is automatically set as a listener for the 'connection' event.
There are two reasons, why the data event is not triggered:
The "named-pipes" package is internally creating sub-pipes. This package is easy to use, if you create the server with the same package. But in this case the pipe server is created via a .net application. So in the javascript code you better use the "net" module of Node.js to connect to the server.
In the .net application you should not create a new StreamWriter. Just use the write method of the server instance. The NamedPipeServerStream implements IDisposable, so it is better to put it in a using block.
.Net Code
static void Main(string[] args)
{
using (NamedPipeServerStream server = new NamedPipeServerStream("XYZNamedPipe"))
{
Console.WriteLine("Waiting for Connection");
server.WaitForConnection();
Console.WriteLine("Connection Established");
int cnt = 0;
while (true)
{
string line = Console.ReadLine();
byte[] messageBytes = Encoding.UTF8.GetBytes((++cnt).ToString() + ": " + line);
server.Write(messageBytes, 0, messageBytes.Length);
}
}
}
JavaScript Code
const net = require('net');
const PIPE_NAME = 'XYZNamedPipe';
const PIPE_PATH = '\\\\.\\pipe\\';
const client = net.createConnection(PIPE_PATH + PIPE_NAME, () => {
console.log('connected to server!');
});
client.on('data', (data) => {
console.log(data.toString());
});
client.on('end', () => {
console.log('disconnected from server');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With