window.ENTITIES={'/api/snippets/javascript/node.js%20-%20create%20named%20pipe%20under%20windows':[{"result":true,"message":null,"batch":{"type":"javascript","name":"node.js - create named pipe under windows","items":[{"id":"pJaoYD","type":"javascript","name":"Node.js - create named pipe under Windows","content":"// -------------------------------------------------\n// server.js file:\n// -------------------------------------------------\n\nconst net = require('net');\n\nconst server = net.createServer((stream) => {\n console.log('Client connected ...');\n stream.on('data', (data) => {\n const text = data.toString();\n console.log('Client message:', text); // should be 'Hi Server!' message from client\n });\n stream.on('end', () => {\n console.log('Client disconnected ...');\n server.close(); // stops pipe server\n });\n stream.write('Hi Client!'); // sends text to client\n});\n\nserver.on('close', () => {\n console.log('Server stopped ...');\n});\n\nconst pipe = \"\\\\\\\\.\\\\pipe\\\\my-pipe\"; // change 'my-pipe' to yours pipe name // equivalent to Unix /tmp/my-pipe.sock\n\nserver.listen(pipe, () => {\n console.log('Server started ...');\n});\n\n\n// Output:\n//\n// Server started ...\n// Client connected ...\n// Client message: Hi Server!\n// Client disconnected ...\n// Server stopped ...\n\n\n\n// -------------------------------------------------\n// client.js file:\n// -------------------------------------------------\n\nconst net = require('net');\n\nconst pipe = \"\\\\\\\\.\\\\pipe\\\\my-pipe\"; // change 'my-pipe' to yours pipe name // equivalent to Unix /tmp/my-pipe.sock\n\nconst client = net.connect(pipe, () => {\n console.log('Client connected ...');\n});\n\nclient.on('data', (data) => {\n const text = data.toString();\n console.log('Server message:', text); // should be 'Server message: Hi Client!' \n client.write('Hi Server!');\n client.end(); // disconnects client\n});\n\nclient.on('end', () => {\n console.log('Client disconnected ...');\n});\n\n\n// Output:\n//\n// Client connected ...\n// Server message: Hi Client!\n// Client disconnected ...","source":"","author":{"id":"YoWmgD","name":"chelsea","avatar":"1629030395354__YoWmgD__w40px_h40px.png","points":806,"role":"BASIC"},"creationTime":1679784069000,"updateTime":1680100659000,"removalTime":null},{"id":"jvJqlj","type":"javascript","name":"Node.js - create named pipe under Windows","content":"// -------------------------------------------------\n// server.js file:\n// -------------------------------------------------\n\nconst net = require('net');\n\nconst server = net.createServer((stream) => { // called on client connected\n stream.on('data', function(data) { // called on client message\n console.log(data.toString()); // prints client message\n });\n stream.on('end', () => { // called on client disconnected\n server.close(); // stops pipe server\n });\n stream.write('Hi Client!'); // sends text to client\n});\n\nserver.on('close', () => { // called on server stopped\n // ...\n});\n\nconst pipe = \"\\\\\\\\.\\\\pipe\\\\my-pipe\"; // equivalent to Unix /tmp/my-pipe.sock\n\nserver.listen(pipe, () => { // called on server started\n // ...\n});\n\n\n\n// -------------------------------------------------\n// client.js file:\n// -------------------------------------------------\n\nconst net = require('net');\n\nconst pipe = \"\\\\\\\\.\\\\pipe\\\\my-pipe\"; // equivalent to Unix /tmp/my-pipe.sock\n\nconst client = net.connect(pipe, () => { // called on client connected\n // ...\n});\n\nclient.on('data', (data) => {\n console.log(data.toString()); // prints server message\n client.write('Hi Server!'); // sends text to server\n client.end(); // disconnects client\n});\n\nclient.on('end', () => { // called on client disconnected\n // ...\n});\n\n\n\n// See also:\n//\n// 1. https://dirask.com/snippets/Node-js-create-named-pipe-under-Windows-pJaoYD","source":"","author":{"id":"EagXqo","name":"Laylah-Walsh","avatar":"1629141325515__EagXqo__w40px_h40px.jpg","points":624,"role":"BASIC"},"creationTime":1679785292000,"updateTime":1680100422000,"removalTime":null}]}}]};