Skip to content

Instantly share code, notes, and snippets.

Last active February 19, 2024 21:55
Star You must be signed in to star a gist
Save tedmiston/5935757 to your computer and use it in GitHub Desktop.
Node.js TCP client and server example
In the node.js intro tutorial (, they show a basic tcp
server, but for some reason omit a client connecting to it. I added an
example at the bottom.
Save the following server in example.js:
var net = require('net');
var server = net.createServer(function(socket) {
socket.write('Echo server\r\n');
server.listen(1337, '');
And connect with a tcp client from the command line using netcat, the *nix
utility for reading and writing across tcp/udp network connections. I've only
used it for debugging myself.
$ netcat 1337
You should see:
> Echo server
/* Or use this example tcp client written in node.js. (Originated with
example code from */
var net = require('net');
var client = new net.Socket();
client.connect(1337, '', function() {
client.write('Hello, server! Love, Client.');
client.on('data', function(data) {
console.log('Received: ' + data);
client.destroy(); // kill client after server's response
client.on('close', function() {
console.log('Connection closed');
Copy link

ghost commented Sep 29, 2020

The output:

Received: Echo server
Hello, server! Love, Client.
Connection closed

all is fine

Copy link

this gist was really helpful for me, thank you dude

Copy link

Hi guys and gals,
I'm trying to create a simulator using Javascript for the front end which I can do, but I'm having a hard time with Node.js for the backend. When I click on, say, a Cisco router icon, I want the a physical connection to occur between my PC and the physcial router that is connected via ethernet. It is a laboratory router (no internet connection). I was thinking that I should download the NPM package telnet-client and require it in the node.js file. I don't know what to do from there. I assume I have to require net as well. Will someone help me. Please... Pretty please :)

Copy link

terthesz commented Apr 5, 2021

Please use arrow functions!

Copy link

NickTsl commented Jul 15, 2021

hello, how can you prevent DDOS attacks on this code?

Copy link

how to set tcp headers on request and receive in the server

Copy link

how to set tcp headers on request and receive in the server

I don't think you can set headers with TCP. HTTP maybe what you are looking for

Copy link

'Received: ' + data

I also have a same issue.......

Copy link

require("net") is not working in 16.13.1
when i move to 18.x now it says digital envelope routines unsupported

Copy link

you try to connect to another pc port? if yes, make sure to allow inbound firewall

Copy link

this gist was really helpful for me, thank you dude

same! thank you!

Copy link

I have multiple simultaneous connections. how can I write to a particular client if the remotePort and remoteAddress is stored to the DB. the new Socket() command does not work in my case because it creates a new instance of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment