Basic TCP Echo Server with Node.js ’net’ Module

Learn how to create a basic TCP echo server using Node.js ’net’ module, ideal for understanding TCP communication and testing network connections.

Brief Introduction

Basic TCP Echo Server with Node.js ’net’ Module (node)
DESCRIPTION:
A simple TCP server that listens on a specific port and echoes back any data received from the client, using Node.js's 'net' module.

ADDITIONAL NOTES:
This example is ideal for understanding the basics of TCP communication in Node.js. It can be used as a foundation to develop more complex servers or to test network communications.

EXPLANATIONS:
- Node.js's 'net' module allows easy and fast creation of TCP servers and clients.
- The server listens on a specific port and creates a dedicated socket for each new connection.
- When the server receives data from the client, it sends it back exactly as received (echo).
- The server handles events such as 'connection', 'data', 'end', and 'error' to control the communication flow.
- The socket.write() method sends data to the connected client.
- The server closes the connection when the client ends the communication.

Example Code: node

javascript
// Importing the 'net' module to create TCP servers
const net = require('net');

// Defining the port on which the server will listen
const PORTA = 1337;

// Creating the TCP server
const server = net.createServer((socket) => {
  // Log when a client connects
  console.log('Client connected:', socket.remoteAddress + ':' + socket.remotePort);

  // Event: data received from the client
  socket.on('data', (data) => {
    console.log('Data received from client:', data.toString());
    // Echo: send back the received data
    socket.write(data);
  });

  // Event: client closes the connection
  socket.on('end', () => {
    console.log('Client disconnected:', socket.remoteAddress + ':' + socket.remotePort);
  });

  // Handling socket errors
  socket.on('error', (err) => {
    console.error('Socket error:', err.message);
  });
});

// Starting the server and listening on the defined port
server.listen(PORTA, () => {
  console.log(`TCP server listening on port ${PORTA}`);
});

// Handling general server errors
server.on('error', (err) => {
  console.error('Server error:', err.message);
});
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Node.js installed (version 12 or higher recommended)

Node.js 'net' module (built-in, no additional installation needed)

TCP port configuration (e.g., 1337) for server listening

TCP client for testing (e.g., netcat, telnet, or a custom client)

No external hardware required (software environment only)

Firewall configuration: ensure the chosen TCP port is open for incoming connections

Similar Topics