1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

How to create TCP client in NodeJS using async/await?

Discussão em 'Angular' iniciado por meallhour, Maio 27, 2025.

  1. meallhour

    meallhour Guest

    I have written the following tcp client in nodejs.

    const net = require('net');

    const HOST = 'linux345';
    const PORT = 2345;
    let ErrCode = 1;

    const client = new net.Socket();

    client.connect(PORT, HOST, function() {
    ErrCode = 0;
    });

    client.on('data', function(data) {
    console.log('Client received: ' + data);
    if (data.toString().endsWith('exit')) {
    client.destroy();
    }
    });

    client.on('close', function() {
    });

    client.on('error', function(err) {
    ErrCode = err.code;
    console.log(ErrCode);
    });

    console.log(ErrCode);


    Please suggest how can I write same logic using async/await

    I have looked into the following post but it is not helpful. node 7.6 async await issues with returning data

    Continue reading...

Compartilhe esta Página