Walkthrough of TCP Server with Node.js Tutorial
by Richard Eitz
Topics covered:
- Setup and configuration of a Node.js app
- Understanding how TCP works
- Handling incoming connections and messages
- Managing clients
- Broadcasting events to clients
A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to.
Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request.
The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. To make a connection request, the client tries to rendezvous with the server on the server's machine and port. The client also needs to identify itself to the server so it binds to a local port number that it will use during this connection. This is usually assigned by the system.
If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.
On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server.
The client and server can now communicate by writing to or reading from their sockets.
An endpoint is a combination of an IP address and a port number. Every TCP connection can be uniquely identified by its two endpoints. That way you can have multiple connections between your host and the server.
Transmission Control Protocol provides reliable, ordered, and error-checked delivery of a stream of octets between apps running on hosts communicating over an IP network.
In other words the protocol can guarantee that what you send through the network and over IP is always arrived on the other end. TCP also has the special feature of keeping the connections opened for later reference and usage.
- A program called the server blocks waiting for a client to connect to it
- A client connects to the server at an IP Address and a Port (eg. 127.0.0.1:3000).
- The server accepts the connection. Now the client can keep a reference to the "pipe" that goes to the server and vice versa.
- The server and client exchange information until they're done.
- The client and server both close their connection. The connection can be closed by any side.
Additional Notes:
- Hosts have ports numbered from 0-65535. Servers listen on a port.
- Multiple clients can be communicating with a server on a given port. Each client connection is assigned a separate socket on that port.
- Client applications get a port and a socket on the client machine when they connect successfully with a server.
index.js
- Import TCP library from the Node.js default libs
'net'
. - Setup default configuration for
PORT
andADDRESS
values. - Create a new TCP
server
and pass as a callbackonClientConnected
which will be executed whenever a new client connects toserver
.
To run:
- Set the file to be executed with
chmod +x index.js
. - Start the server with
npm start
. - Receive
Server started at: 127.0.0.1:5000
message. - Test using telnet. Run
telnext 127.0.0.1 5000
in another terminal. - Receive a message like
New client: 127.0.0.1:64637
.
Recap: Create a server that accepts a client connection, displayed information about the client and closed the connection. onClientConnected
is executed only once, when the client connects.
index.js
- Set up two events:
socket.on('data', callback )
callscallback
when the server receives data from the client.data
can be a bugger of bytes or a string.socket.on('end', callback )
callscallback
when the client disconnects from the server.
To run:
- Run
telnext 127.0.0.1 5000
. - Write a message to server.
Sample outputs:
- Server
Server started at: 127.0.0.1:5000 127.0.0.1:54052 connected. 127.0.0.1:54052 said: Hey 127.0.0.1:54052 disconnected.
- Client
$ telnet localhost 5000 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Hey We got your message: Hey ^] telnet> q Connection closed.
NB: Broadcasting is the act of sending a message to everyone on the network.
Abstract into Server
and Client
classes.
init.js
- Import
Server
class and start a new instance of aServer
at default configuration.
server.js
- Import TCP library
'net'
andClient
class. constructor(port, address)
sets up values forstart
method and initializes aclients
array that references all connected clients.start(cb)
contains the contents ofonClientConnected
and instantiates aClient
instance and adds it tothis.clients
array.cb
is executed when the server finishes initializing.