Building TCP server in NodeJS

TCP stands for tansmission control protocol this protocol designed to send the informations between servers using ip and port .This protocol exist in layer four Transport layer in OSI Model.

Advantages of using TCP

  • Acknowledgement Which means that when the server send a message it should make sure that these message received or not.
  • Guaranteed delivery When server receive data from another server it should respond that it’s already received.
  • Connection based TCP is a connection based this means that both client and server need to establish connection between them.
  • Congestion control control the traffic of crowded packets or flow of packets.

Disadvantages of using TCP

  • Larger packets This because the more headers added to the packet for acknowledgement and delivery guaranteed also ordering the packets all these headers increases the size.
  • More bandwidth because it’s larger packets.
  • Slower than UDP this is because Congestion control and Ordering the packets which causes a delay or wait also it will waiting for guaranteed delivery all of this make it slower than UDP.
  • Stateful if you shut-down the server all clients connection closed and you can not resume that.
  • Server Memory (DOS) because of connection based the server has to stack up for each connection and allocate memory for each connection also listen for more data did server receive data ? did client still connected? and more more data so this make tcp server has limited connections. This also can be used for denial of service attack by opening multiple connections and the server will be wait and if more and more of connection the server may die and losing the service itself.

Implementation part

we will use NodeJS in implementation part and we will use built-in net module no need for installing third party packages.


  • Make sure that nodeJS & npm installed on your machine.
  • Create a new node project TCPserver .
  • Inside TCPserver directory create a new file server.js

in TCPserver/server.js

const net = require("net");
const server = net.createServer((socket)=>{
    socket.write("Hello From Server!")
        console.log("Connection closed.!!!")


Run the project by typing the fllowing command node server.js

NodeJS TCP server

Then open the terminal and type the following command telnet 8080

Telnet connection

From Terminal write a message then click enter this message will send to the server as appeared in images below

Telnet send message to server

Message received from telnet client

Subscribe to our Newsletter

Subscribe to tech-hour website to get all updates, new articles, new courses and projects. No spam ever. Unsubscribe at any time.