Hi Guys,
In this blog post I am going to explain in detail about Building TCP Servers in NodeJs .

What is NodeJs => Node.js is an open-source server side runtime environment built on Chrome’s V8 JavaScript engine. It provides an event driven, non-blocking (asynchronous) I/O and cross-platform runtime environment for building highly scalable server-side application using JavaScript.
WHAT’S IN THIS BLOG

  • Creating a TCP server
  • Closing server-side TCP connections
  • Handling network errors
  • Piping data to or from a TCP connection
  • Building a TCP chat server

The Transmission Control Protocol (TCP) is one of the fundamental protocols of the Internet. It sits on top of the Internet protocol (IP) and provides a transport mechanism for the application layer. HTTP, for instance, works on top of TCP, as do many other connection-oriented applications such as iRC, SMTP, and IMAP.                                    Node has a first-class HTTP server implementation in the form of a pseudo-class in http.Server , which descends from the TCP server pseudo-class in net.Server .

CREATING A TCP SERVER

You can create a TCP server using the net module like this:

you use the createServer() method on the net package, which you bind to TCP port 4000 ,You can pass in a callback function to createServer to be called every time there is a “connection” event.Inside this callback you will be handed a socket object, which you can use to send and receive data to and from the client.                                      Because the server object is also an EventEmitter, and you can listen to events during its lifecycle, net.Server emits the following events:

“listening” — When the server is listening on the specifi ed port and address.
“connection” — When a new connection is established. The callback to this function will receive the      corresponding socket object. You can also bind to this event by passing a function to server.createServer() .
“close” — When the server is closed, that is, it’s not bound to that port any more.
“error” — When an error occurs at the server level. An error event happens, for instance, when you try to bind   to  an occupied port or to a port you don’t have permission to bind to.

Lifecycle example of a TCP server

You can launch this example and, while the server is running, connect to it by using telnet or nc   like so:
$ telnet localhost 4000
or
$ nc localhost 4000

Once you connect, the server will disconnect you and then close                                                                               You should see the following output on the server console:

Server is listening on port 4000
Server has a new connection
Server is now closed

Using the Socket Object

When you get a “connection” event you are also handed the socket object as the first argument of the callback function. This socket object is both a read and a write stream, which means that it emits “data” events when it gets a package of data and emits the “end” event when that connection is closed.                                                     Because the socket object is also a writable stream, that means you can write buffers or strings to the socket by using socket.write() . You can tell the socket that it should terminate the connection after all data has been written by calling socket.end() .

Again, you can start this server, connect to it using telnet or nc , as you did before, and play around with it. You can type quit<enter> to terminate the connection.

Handling Errors

When handling a socket on the client or the server you can (and should) handle errors by listening to
the error event like this:

If you fail to catch an error, Node will handle an uncaught exception and terminate the current process.

BUILDING A SIMPLE TCP CHAT SERVER

Accepting Connections

Reading Data from a Connection

Collecting All the Clients
Creating a chat server in which you have to broadcast the user data to everyone

Broadcasting Data
Every time a connected user types anything, you need to send it to every other connected user.

Removing Closed Connections
We have to remove the connection when it gets closed

 Use Your TCP Chat Server

Now you should be ready to test your server. You can save the fi e to chat_server.js and launch it
through the command line:
$ node chat_server.js
You can now connect to that server using nc or telnet :
$ nc localhost 4000
If you can, try launching several of these in separate terminal windows. You can then see the chat
server in action.