How do I return values in a socket?

Asked

Viewed 15 times

0

I am receiving data from a C client, but when I make the necessary changes, the returned value is not printed. Only when I do a console.log. However, I would like the return to occur after c.write..

inserir a descrição da imagem aqui

let net = require('net');

let server = net.createServer(function(c) { //'connection' listener
  console.log('Cliente conectado.');
  c.on('end', function() {
    console.log('Cliente disconectado.');
  });
  c.on('data', function(buffer){
    if (buffer[0] === 1){
      let number = buffer.toString();
      if (Number.isNaN(number)) {
          c.write('Na opção 1 é necessário enviar um inteiro na msg', "utf-8");
      }
      else {
          c.write(number);
          console.log(number);
      }
    }
    else if (buffer[0] === 2){
      let char = buffer.toString();

      if (char.length !== 1) {
        c.write('Na opção 2 é necessário enviar apenas um caracter', 'utf-8');
      }
      else {
          var asciiNumber = char.charCodeAt();
          if (asciiNumber === char.toLowerCase().charCodeAt()) {
              c.write(char.toLowerCase, "utf-8");
          }
          else {
              c.write(char.toUpperCase, "utf-8");
          }
      }
      
    }
    else if (buffer[0] === 3){
      let sentence = buffer.toString();
      let inverted = sentence.split("").reduce((rev, char)=> char + rev, ''); 
      console.log(inverted);
      c.write(inverted);
    }
    else{
      console.log("Erro");
    }
  })
  
  c.on('error', function(err){
    console.log(err);
  })
});
server.listen(9925, function() { //'listening' listener
  console.log('Servidor online');
});
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.