Skip to content

Instantly share code, notes, and snippets.

@getnamo
Created October 16, 2018 09:42
Show Gist options
  • Save getnamo/396577cb4988188e291774ac7e368368 to your computer and use it in GitHub Desktop.
Save getnamo/396577cb4988188e291774ac7e368368 to your computer and use it in GitHub Desktop.
/** from: https://nodejs.org/api/net.html#net_net_createconnection_options_connectlistener */
const net = require('net');
const port = 3000;
const client = net.createConnection({ port: 3000 }, () => {
console.log('connected to server!');
//pipe immediately something back
client.write('Hello from TCP client!\r\n');
//send delayed data test
setTimeout(()=>{
console.log('sent!');
client.write('and some delayed text.\r\n');
}, 2000);
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});
client.on('error',(err)=>{
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment