Implementing a TCP Socket Client

At the most basic level, implementing a TCP socket client involves creating a Socket object that connects to the server and then writing data to the server and handling the data that comes back. In addition, you should build the socket so that it can also handle errors, the buffer being full, and timeouts. This section discusses the steps involved in implementing a socket client using the Socket object. Listing 8.1 provides the full code for the following discussion.

The first step is to create the socket client by calling net.connect(), as shown below. Pass in the port and host that you want to connect to as well and implement a callback function to handle the connect event:

net.connect({port: ...

Get Node.js, MongoDB, and AngularJS Web Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.