Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
socket.remoteAddress is undefined when there is a timeout or an error

I made this promise function which makes a connection to a specific host however I need to have the remoteAddress to be present in the logs when there is a timeout or an error. It seems like this gets deleted from the object. This is my code:

const makeTCPConnection = async (host, portNumber) =>
  new Promise((resolve, reject) => {
    const client = new Socket();
    let remoteAddress;
    let localAddress;
    const timeout = 2;

    client.setTimeout(timeout);

    client.connect(portNumber, host, () => {
      console.log("TCP connection established", {
        host,
        portNumber,
        localAddress,
        remoteAddress
      });
    });

    client.on("connect", () => {
      localAddress = client.localAddress;
      remoteAddress = client.remoteAddress;
      // client.destroy();
      resolve();
    });

    client.on("close", () => {
      client.destroy();
      resolve();
    });

    client.on("drain", () => {
      client.destroy();
      resolve();
    });

    client.on("end", () => {
      client.destroy();
      resolve();
    });

    client.on("timeout", (error) => {
      console.error("TCP client timeout after 1s", {
        error,
        host,
        portNumber,
        localAddress,
        remoteAddress
      });
      // client.destroy();
      reject(error);
    });

    client.on("error", (error) => {
      console.error("TCP client connection error", {
        error,
        host,
        portNumber,
        localAddress,
        remoteAddress
      });
      client.destroy();
      reject(error);
    });
  });

I been testing this with timeout mainly, I tried to remove the client.destroy() on timeout event but then I get like 4 logs before I get the one with remoteAddress on it. The same thing is happening with localAddress.

Anyone have any ideas?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda