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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.