Created
August 29, 2023 20:25
-
-
Save bmatusiak/1b8a68dfa5e4d53165618fae2aa17e14 to your computer and use it in GitHub Desktop.
basic-uds-net.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require('net'); | |
const path = require('path'); | |
var listen_path = path.join(process.cwd(), 'nodejs.socket'); | |
if (process.platform.indexOf("win") > -1) | |
listen_path = path.join('\\\\?\\pipe', process.cwd(), 'nodejs.socket'); | |
module.exports = { | |
server: function () { | |
var server = net.createServer((socket) => { | |
console.log('client connected'); | |
socket.on('data', (data) => { | |
console.log(data.toString()); | |
socket.write('world!'); | |
}); | |
socket.on('end', () => { | |
console.log('client disconnected'); | |
}); | |
setTimeout(()=>{ | |
socket.end('goodbye'); | |
},3000); | |
}).on('error', (err) => { | |
throw err; | |
}); | |
server.listen(listen_path, () => { | |
console.log('opened server on', server.address()); | |
}); | |
}, | |
client: function () { | |
var client = net.createConnection(listen_path, () => { | |
console.log('connected to server!'); | |
client.write('hello!'); | |
}); | |
client.on('data', (data) => { | |
console.log(data.toString()); | |
client.end(); | |
}); | |
client.on('end', () => { | |
console.log('disconnected from server'); | |
}); | |
} | |
} | |
if (process.argv[2] == "c") | |
module.exports.client(); | |
else | |
module.exports.server(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment