node.js - create named pipe under windows
JavaScript[Edit]
+
0
-
0
Node.js - create named pipe under Windows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69// ------------------------------------------------- // server.js file: // ------------------------------------------------- const net = require('net'); const server = net.createServer((stream) => { console.log('Client connected ...'); stream.on('data', (data) => { const text = data.toString(); console.log('Client message:', text); // should be 'Hi Server!' message from client }); stream.on('end', () => { console.log('Client disconnected ...'); server.close(); // stops pipe server }); stream.write('Hi Client!'); // sends text to client }); server.on('close', () => { console.log('Server stopped ...'); }); const pipe = "\\\\.\\pipe\\my-pipe"; // change 'my-pipe' to yours pipe name // equivalent to Unix /tmp/my-pipe.sock server.listen(pipe, () => { console.log('Server started ...'); }); // Output: // // Server started ... // Client connected ... // Client message: Hi Server! // Client disconnected ... // Server stopped ... // ------------------------------------------------- // client.js file: // ------------------------------------------------- const net = require('net'); const pipe = "\\\\.\\pipe\\my-pipe"; // change 'my-pipe' to yours pipe name // equivalent to Unix /tmp/my-pipe.sock const client = net.connect(pipe, () => { console.log('Client connected ...'); }); client.on('data', (data) => { const text = data.toString(); console.log('Server message:', text); // should be 'Server message: Hi Client!' client.write('Hi Server!'); client.end(); // disconnects client }); client.on('end', () => { console.log('Client disconnected ...'); }); // Output: // // Client connected ... // Server message: Hi Client! // Client disconnected ...
[Edit]
+
0
-
0
Node.js - create named pipe under Windows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55// ------------------------------------------------- // server.js file: // ------------------------------------------------- const net = require('net'); const server = net.createServer((stream) => { // called on client connected stream.on('data', function(data) { // called on client message console.log(data.toString()); // prints client message }); stream.on('end', () => { // called on client disconnected server.close(); // stops pipe server }); stream.write('Hi Client!'); // sends text to client }); server.on('close', () => { // called on server stopped // ... }); const pipe = "\\\\.\\pipe\\my-pipe"; // equivalent to Unix /tmp/my-pipe.sock server.listen(pipe, () => { // called on server started // ... }); // ------------------------------------------------- // client.js file: // ------------------------------------------------- const net = require('net'); const pipe = "\\\\.\\pipe\\my-pipe"; // equivalent to Unix /tmp/my-pipe.sock const client = net.connect(pipe, () => { // called on client connected // ... }); client.on('data', (data) => { console.log(data.toString()); // prints server message client.write('Hi Server!'); // sends text to server client.end(); // disconnects client }); client.on('end', () => { // called on client disconnected // ... }); // See also: // // 1. https://dirask.com/snippets/Node-js-create-named-pipe-under-Windows-pJaoYD