Get your own Node server
const net = require('net');
const { EOL } = require('os');

// Configuration
const PORT = 3009;
const MAX_CONNECTIONS = 5;
const WELCOME_MESSAGE = `Welcome to the TCP Server!${EOL}Type 'exit' to disconnect.${EOL}Type 'help' for available commands.${EOL}`;

// Store connected clients
const clients = new Set();

// Create a TCP server
const server = net.createServer((socket) => {
  // Client connection handler
  const clientId = `${socket.remoteAddress}:${socket.remotePort}`;
  console.log(`Client connected: ${clientId}`);
  
  // Add to clients set
  clients.add(socket);
  
  // Send welcome message
  socket.write(WELCOME_MESSAGE);
  
  // Handle incoming data
  socket.on('data', (data) => {
    const input = data.toString().trim();
    console.log(`[${clientId}] ${input}`);
    
    // Handle commands
    switch (input.toLowerCase()) {
      case 'exit':
        socket.write('Goodbye!\n');
        socket.end();
        break;
        
      case 'help':
        socket.write('Available commands:\n- help: Show this help\n- time: Show server time\n- clients: Show connected clients\n- exit: Disconnect\n');
        break;
        
      case 'time':
        socket.write(`Server time: ${new Date().toISOString()}\n`);
        break;
        
      case 'clients':
        const clientCount = clients.size;
        socket.write(`Connected clients: ${clientCount}\n`);
        break;
        
      default:
        // Echo back the received data
        socket.write(`You said: ${input}${EOL}`);
    }
  });
  
  // Handle client disconnection
  socket.on('end', () => {
    console.log(`Client disconnected: ${clientId}`);
    clients.delete(socket);
  });
  
  // Handle errors
  socket.on('error', (err) => {
    console.error(`Socket error (${clientId}):`, err.message);
    clients.delete(socket);
  });
});

// Server events
server.on('listening', () => {
  const addr = server.address();
  console.log(`TCP Server listening on ${addr.address}:${addr.port}`);
  console.log('Use a tool like telnet or netcat to connect');
  console.log('Example: telnet localhost 3009');
});

server.on('error', (err) => {
  console.error('Server error:', err);
  process.exit(1);
});

// Handle process termination
process.on('SIGINT', () => {
  console.log('\nShutting down server...');
  
  // Close all client connections
  console.log('Disconnecting all clients...');
  for (const client of clients) {
    client.write('Server is shutting down. Goodbye!\n');
    client.end();
  }
  
  // Close the server
  server.close(() => {
    console.log('Server closed');
    process.exit(0);
  });
});

// Start the server
server.maxConnections = MAX_CONNECTIONS;
server.listen(PORT, '0.0.0.0', () => {
  console.log(`Server started with max ${MAX_CONNECTIONS} connections`);
});

              
Server started with max 5 connections
TCP Server listening on 0.0.0.0:3009
Use a tool like telnet or netcat to connect
Example: telnet localhost 3009

Client connected: ::1:54321
[::1:54321] help
[::1:54321] time
[::1:54321] clients
[::1:54321] Hello, server!
[::1:54321] exit
Client disconnected: ::1:54321

Shutting down server...
Disconnecting all clients...
Server closed