{{announcement.body}}
{{announcement.title}}

TCP With Node

DZone 's Guide to

TCP With Node

In this post, we will learn to build a TCP server using Node.js.

· Web Dev Zone ·
Free Resource

Introduction

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.

Recap

Lets go through some of the networking related terminologies which we’ve discussed in much details in previous posts:

Network layers

  • 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.

Node Networking

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

Demo
You want to start your own TCP server, bind to a port, and send data over the network.

Solution

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.

Creating a server in Node


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. 

Creating simple server in Node

You can access the code on this git repository.

Now, we can run this tcp server and following screen will show up:

Server listening on port 8000

Now run the PacketSender application and connect to Node TCP server as follows:

GET request to PacketSender

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:

starting server

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: 

networking tab in console

Summary

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

  • https://hexquote.com/network-programming-setting-the-scene/
  • https://hexquote.com/pretty-little-things-tcp-ip-ports-and-sockets/
  • https://hexquote.com/networking-basics/
  • https://github.com/jawadhasan/nodetcp
Topics:
iintegration, networking, node.js

Published at DZone with permission of Jawad Hasan Shani . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}