Skip to content

Instantly share code, notes, and snippets.

@roppa
Created July 12, 2017 18:29
Show Gist options
  • Select an option

  • Save roppa/b7536da308dd7d58de723f75b10bad07 to your computer and use it in GitHub Desktop.

Select an option

Save roppa/b7536da308dd7d58de723f75b10bad07 to your computer and use it in GitHub Desktop.
Node.js socket server and client
const net = require('net');
const PORT = 6000;
const server = net.createServer((connection) => {
console.log('created server');
connection.on('data', (data) => {
connection.write('pong\n');
});
connection.pipe(connection);
});
server.listen(PORT);
const client = net.createConnection({ port: PORT }, () => {
console.log('created client');
setInterval(() => {
client.write('ping');
}, 1000);
});
client.on('data', (data) => {
console.log('client got data', data.toString());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment