TCP With Node
In this post, we will learn to build a TCP server using Node.js.
Join the DZone community and get the full member experience.Join For Free
Everyone who has any knowledge of the internet is well-aware of the Transport Control Protocol (TCP). Robert Kahn and Vinton Cerf invented TCP in the 1970s for satellite communications. In their work building a satellite communications network, they realized the need for a hardware-independent mechanism to move data from one computer to another. All prior work – remember, there was no internet – used systems highly customized for specific hardware implementations. They thought that a more general mechanism would make it easier to add connectivity to new systems.
They built TCP as a general communication mechanism that could easily be adapted to different computer systems. They architected it as a connection-oriented protocol that managed connections between client devices (initiators) and servers (responders).
TCP is much improved over those early days. It is now part of the TCP/IP communication suite that is part of nearly every computer system on the planet.
I’ve previously written about networking, TCP and node in various posts. The networking stack itself is very standard and all the popular programming languages have support to program TCP Servers and clients.
In this post I will write a basic TCP Server to understand some of the building blocks when implementing this functionality. I choose Node for this demo, however you can implement it in other programming languages as well.
Lets go through some of the networking related terminologies which we’ve discussed in much details in previous posts:
- HTTP: Hypertext Transfer Protocol—An application-layer client-server protocol built on TCP.
- TCP : Transmission Control Protocol—Allows communication in both directions from the client to the server, and is built on to create application-layer protocols like HTTP.
- Socket: The combination of an IP address and a port number is generally referred to as a socket.
- Packet: TCP packets are also known as segments—the combination of a chunk of data along with a header.
As software developers, you mostly work at a higher level than lower-level hardware. When
talking to networks, we’re concerned with the application and transport layers of the Internet Protocol (IP) suite.
You can make TCP connections with Node by using the net module. This allows
you to implement application layer protocols and this is the one we will be using in our code today.
If you find yourself needing to talk to nonstandard TCP protocols, perhaps something used internally in your company, then net.Socket and net.createConnection will make
light work of it.
TCP clients and servers:
Node has a simple API for creating TCP connections and servers. Most of the lowest level classes and methods can be found in the net module.
Demo – Creating a TCP server and tracking clients
You want to start your own TCP server, bind to a port, and send data over the network.
Use net.createServer to create a server, and then call server.listen to bind it to a port.
To connect to the server, either use the command-line tool telnet or create an in-process client connection with its client counterpart, net.connect. However, I’ll use a tool called PacketSender for this example.
Node TCP Server code
Here is the start up code shell, we start with net library and created a server and then bind this server to a port and start listening for incoming request.
The callback function in createServer is the place were we will put our code to handle incoming TCP connection. The code below is very basic and self explanatory.
Following is the callback function code. Again the code is very simple and we are just doing some basic tracking and sending back some data to client.
You can access the code on this git repository.
Now, we can run this tcp server and following screen will show up:
Now run the PacketSender application and connect to Node TCP server as follows:
We entered Address, port for our TCP server, then entered the ASCII text and click the send button.
Check console log on node application which shows the connection information:
You will also find some log information on the client software (PacketSender) as well which will show us the info including data which sever send us back as follows:
This was a very simple but practical application of building a TCP Sever using Node. Let me know of your comments and suggestions. Till next time, happy coding.
References and Related Links
Published at DZone with permission of Jawad Hasan Shani. See the original article here.
Opinions expressed by DZone contributors are their own.
What Is Test Pyramid: Getting Started With Test Automation Pyramid
Build a Simple Chat Server With gRPC in .Net Core
Building A Log Analytics Solution 10 Times More Cost-Effective Than Elasticsearch
Why You Should Consider Using React Router V6: An Overview of Changes