Computer scienceBackendNode.jsCore ConceptsInternal modulesOther modules

Introduction to net module

TCP Client Message Echo

Report a typo

Suppose you have created a TCP client that sends a message to a server and expects a response. The server echoes back the received message to the client. Your client code is as follows:

const net = require('net');

const client = net.createConnection({ port: 3000, host: 'localhost' }, () => {
  console.log('Connected to server');

  client.write('Hello, server!');
});

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

  // Close the connection after receiving data
  client.end();
});

Assuming the server echoes the message "Hello, server!" back to the client, what will be the output of the client code?

Please provide your answer in the text box below.

Enter a short text

Create a free account to access the full topic