Unveiling the Power of TCP: Building Apps with Node.js's net Module

Unveiling the Power of TCP: Building Apps with Node.js's net Module

ยท

3 min read

The net module in Node.js allows you to build TCP applications by creating both TCP servers and clients. TCP (Transmission Control Protocol) is a reliable protocol that ensures ordered and error-free data transmission over a network.

Here's a breakdown of what you can do with the net module:

1. TCP Servers:

  • You can create a TCP server using net.createServer(). This function takes a callback function that executes whenever a client connects to the server.

  • Inside the callback function, you can handle events like:

    • 'data': This event fires when the server receives data from the client.

    • 'close': This event fires when the client disconnects.

  • You can use the socket.write() method to send data back to the connected client.

2. TCP Clients:

  • You can create a TCP client using net.createConnection(). This function takes the server's address (IP and port) and a callback function that executes when the connection is established.

  • The callback function allows you to send data to the server using socket.write().

  • You can listen for the 'data' event to receive data from the server.

This example demonstrates a simple TCP server and client application built with Node.js's net module.

Server (server.js):

const net = require('net');

const port = 8080; // Port to listen on

const server = net.createServer((socket) => {
  console.log('Client connected!');

  // Handle incoming data from the client
  socket.on('data', (data) => {
    console.log(`Received data from client: ${data.toString()}`);

    // Send a response back to the client
    socket.write(`Hello from the server!`);
  });

  // Handle client disconnection
  socket.on('end', () => {
    console.log('Client disconnected!');
  });
});

server.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});

Client (client.js):

const net = require('net');

const host = 'localhost'; // Server hostname (or IP)
const port = 8080; // Port to connect to

const client = new net.Socket();

// Handle connection to the server
client.connect(port, host, () => {
  console.log('Connected to server!');

  // Send data to the server
  client.write('Hello from the client!');
});

// Handle data received from the server
client.on('data', (data) => {
  console.log(`Received data from server: ${data.toString()}`);
});

// Handle client disconnection (optional)
client.on('end', () => {
  console.log('Disconnected from server!');
});
  1. Save the server code as server.js and the client code as client.js.

  2. Run the server first: node server.js

  3. In a separate terminal, run the client: node client.js

This will establish a TCP connection between the client and server. The server will log messages when a client connects,receives data, and disconnects. The client will log similar messages and also send a message "Hello from the client!" to the server upon connection. The server will then respond with "Hello from the server!".

Note:

  • Remember, the net module can only be used in a Node.js environment, not directly in a web browser.

  • Also, remember to replace localhost with the actual server IP address if you're running the server on a different machine.

Let's wrap up things

By understanding these concepts, you can build more complex TCP-based applications in Node.js!

HAPPY CODING ๐Ÿš€

ย