Building  TCP Client

Hello All,

TCP is one of the most used transport protocols on the Internet, on top of which sit application protocols like HTTP. TCP is a connection-oriented protocol, which means that one endpoint requests and establishes a dedicated connection to another endpoint. This connection is a two-way dedicated stream into which both endpoints can send and receive data to/from each other at the same time. TCP guarantees that the messages you receive are in order. It has built-in mechanisms for flow control, making it a good platform for supporting machine-to-machine and human-to-machine protocols.

NOTE  : One point you should keep in mind is that when you write to a TCP stream, you receive no  acknowledgment that the other side has received the data. Even worse, because the underlying network implementation may chop and  route your packets haphazardly, the other endpoint may have received only part of the message you sent (even though TCP internally tries to resend the packets that have been lost or corrupted). Remember that TCP guarantees only that the packets are received in order by the application, not that they are received at all. Furthermore, TCP is not message-oriented; it just provides a continuous stream of data. If you need to distinguish individual messages, you need to implement a framing protocol that specifies where each message starts and ends. HTTP, for instance, solves these problems whereby an application needs a request-response workflow from a server and request and response messages are clearly delimited.


when the connection is established by passing a callback function as the last argument for notified  Connected

You can instead listen for the connect event emitted by the conn object:


The return value of the call to net.createConnection() is an instance of net.Socket , which represents the connection to the server and is both a readable and a writeable stream. It allow to send some Data

We can receive data from the server by listening to the data event emitted by the connection every time data is available:


we can close the connection from your endpoint by using the connection end method , you can also send some data (a buffer or a string)


Errors can happen when you are trying to establish a connection — the hostname is not found on DNS, the target host is not reachable, or the connection is rejected — or a network error happens when you already had an established connection. You can trap errors by listening to the error event

Building  TCP Client Example :

Save  this code with File Name of client.js

$ node client.js  Run on Terminal

Save  this code with File Name of server.js

$ node server.js  Run on Terminal

Thanks To All

For More Please See and