Skip to content

Instantly share code, notes, and snippets.

@sid24rane

sid24rane/net.js

Last active Nov 16, 2020
Embed
What would you like to do?
Simple TCP Client and Server in Node.js (Covering all useful Properties & Methods)
var net = require('net');
// creates the server
var server = net.createServer();
//emitted when server closes ...not emitted until all connections closes.
server.on('close',function(){
console.log('Server closed !');
});
// emitted when new client connects
server.on('connection',function(socket){
//this property shows the number of characters currently buffered to be written. (Number of characters is approximately equal to the number of bytes to be written, but the buffer may contain strings, and the strings are lazily encoded, so the exact number of bytes is not known.)
//Users who experience large or growing bufferSize should attempt to "throttle" the data flows in their program with pause() and resume().
console.log('Buffer size : ' + socket.bufferSize);
console.log('---------server details -----------------');
var address = server.address();
var port = address.port;
var family = address.family;
var ipaddr = address.address;
console.log('Server is listening at port' + port);
console.log('Server ip :' + ipaddr);
console.log('Server is IP4/IP6 : ' + family);
var lport = socket.localPort;
var laddr = socket.localAddress;
console.log('Server is listening at LOCAL port' + lport);
console.log('Server LOCAL ip :' + laddr);
console.log('------------remote client info --------------');
var rport = socket.remotePort;
var raddr = socket.remoteAddress;
var rfamily = socket.remoteFamily;
console.log('REMOTE Socket is listening at port' + rport);
console.log('REMOTE Socket ip :' + raddr);
console.log('REMOTE Socket is IP4/IP6 : ' + rfamily);
console.log('--------------------------------------------')
//var no_of_connections = server.getConnections(); // sychronous version
server.getConnections(function(error,count){
console.log('Number of concurrent connections to the server : ' + count);
});
socket.setEncoding('utf8');
socket.setTimeout(800000,function(){
// called after timeout -> same as socket.on('timeout')
// it just tells that soket timed out => its ur job to end or destroy the socket.
// socket.end() vs socket.destroy() => end allows us to send final data and allows some i/o activity to finish before destroying the socket
// whereas destroy kills the socket immediately irrespective of whether any i/o operation is goin on or not...force destry takes place
console.log('Socket timed out');
});
socket.on('data',function(data){
var bread = socket.bytesRead;
var bwrite = socket.bytesWritten;
console.log('Bytes read : ' + bread);
console.log('Bytes written : ' + bwrite);
console.log('Data sent to server : ' + data);
//echo data
var is_kernel_buffer_full = socket.write('Data ::' + data);
if(is_kernel_buffer_full){
console.log('Data was flushed successfully from kernel buffer i.e written successfully!');
}else{
socket.pause();
}
});
socket.on('drain',function(){
console.log('write buffer is empty now .. u can resume the writable stream');
socket.resume();
});
socket.on('error',function(error){
console.log('Error : ' + error);
});
socket.on('timeout',function(){
console.log('Socket timed out !');
socket.end('Timed out!');
// can call socket.destroy() here too.
});
socket.on('end',function(data){
console.log('Socket ended from other end!');
console.log('End data : ' + data);
});
socket.on('close',function(error){
var bread = socket.bytesRead;
var bwrite = socket.bytesWritten;
console.log('Bytes read : ' + bread);
console.log('Bytes written : ' + bwrite);
console.log('Socket closed!');
if(error){
console.log('Socket was closed coz of transmission error');
}
});
setTimeout(function(){
var isdestroyed = socket.destroyed;
console.log('Socket destroyed:' + isdestroyed);
socket.destroy();
},1200000);
});
// emits when any error occurs -> calls closed event immediately after this.
server.on('error',function(error){
console.log('Error: ' + error);
});
//emits when server is bound with server.listen
server.on('listening',function(){
console.log('Server is listening!');
});
server.maxConnections = 10;
//static port allocation
server.listen(2222);
// for dyanmic port allocation
server.listen(function(){
var address = server.address();
var port = address.port;
var family = address.family;
var ipaddr = address.address;
console.log('Server is listening at port' + port);
console.log('Server ip :' + ipaddr);
console.log('Server is IP4/IP6 : ' + family);
});
var islistening = server.listening;
if(islistening){
console.log('Server is listening');
}else{
console.log('Server is not listening');
}
setTimeout(function(){
server.close();
},5000000);
//---------------------client----------------------
// creating a custom socket client and connecting it....
var client = new net.Socket();
client.connect({
port:2222
});
client.on('connect',function(){
console.log('Client: connection established with server');
console.log('---------client details -----------------');
var address = client.address();
var port = address.port;
var family = address.family;
var ipaddr = address.address;
console.log('Client is listening at port' + port);
console.log('Client ip :' + ipaddr);
console.log('Client is IP4/IP6 : ' + family);
// writing data to server
client.write('hello from client');
});
client.setEncoding('utf8');
client.on('data',function(data){
console.log('Data from server:' + data);
});
setTimeout(function(){
client.end('Bye bye server');
},5000);
//NOTE:--> all the events of the socket are applicable here..in client...
// -----------------creating client using net.connect instead of custom socket-------
// server creation using net.connect --->
// u can also => write the below code in seperate js file
// open new node instance => and run it...
const clients = net.connect({port: 2222}, () => {
// 'connect' listener
console.log('connected to server!');
clients.write('world!\r\n');
});
clients.on('data', (data) => {
console.log(data.toString());
clients.end();
});
clients.on('end', () => {
console.log('disconnected from server');
});
@adrwh

This comment has been minimized.

Copy link

@adrwh adrwh commented Feb 18, 2019

Epic, looks awesome.. how would the client write subsequent messages back to the server, like in a tcp chat room style?

@atesztoth

This comment has been minimized.

Copy link

@atesztoth atesztoth commented Mar 27, 2020

Nice gist!

@thisisarjun

This comment has been minimized.

Copy link

@thisisarjun thisisarjun commented Apr 3, 2020

Awesome.

@AdharshKrish

This comment has been minimized.

Copy link

@AdharshKrish AdharshKrish commented Apr 4, 2020

Thankyou so much. This is even better than nodejs documentation

@phifogg

This comment has been minimized.

Copy link

@phifogg phifogg commented Apr 30, 2020

Epic, looks awesome.. how would the client write subsequent messages back to the server, like in a tcp chat room style?

same question... subscribing

@fdl333

This comment has been minimized.

Copy link

@fdl333 fdl333 commented Jul 22, 2020

I get:
Error [ERR_SERVER_ALREADY_LISTEN]: Listen method has been called more than once without closing.

As soon as I launch the program ... tried googling, seems a common problem, but found no solution. ANy help pls?

@andreiskandar

This comment has been minimized.

Copy link

@andreiskandar andreiskandar commented Aug 29, 2020

I am new to this concept and currently doing a project to use 'net' and 'fs' module from node where a client request a file from the server then the server looks for the requested file locally. if file exists and found, then the server sends back the data to the client. I wonder what socket event listener should I use to complete this query?

@wcoffn-livly

This comment has been minimized.

Copy link

@wcoffn-livly wcoffn-livly commented Oct 1, 2020

I get:
Error [ERR_SERVER_ALREADY_LISTEN]: Listen method has been called more than once without closing.

As soon as I launch the program ... tried googling, seems a common problem, but found no solution. ANy help pls?

Same issue - any help would be appreciated. Running with Node v12 on Windows.

@sampirat

This comment has been minimized.

Copy link

@sampirat sampirat commented Oct 20, 2020

I get:
Error [ERR_SERVER_ALREADY_LISTEN]: Listen method has been called more than once without closing.
As soon as I launch the program ... tried googling, seems a common problem, but found no solution. ANy help pls?

Same issue - any help would be appreciated. Running with Node v12 on Windows.

Lines 131 & 135-143 : choose one method for listening (static or dynamic) and comment out the other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.