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

250
Visualizações
Node.js click button and send something with TCP/IP protocol

I am new in node. I am trying to make the application where user can click on button and something will go to the client with TCP.
Here is my ECHO server (example from nodejs.org)

const net = require('net');

var HOST = '0.0.0.0'
var PORT = 6969;

var server = net.createServer();
server.listen(PORT, HOST);
console.log('Server listening on ' + server.remoteAddress + ':' + server.remotePort);

server.on('connection', function(socket) {
  console.log('CONNECTED: ' + socket.remoteAddress + ':' + socket.remotePort);
  socket.write('Hello from server');
  // other stuff is the same from here
  socket.on('data', function(data) {
    socket.write('reply data ' + data);
  });
});

Could you explain me how to get this connection in another js file. I am trying make module.exports = server, but I can not understand how to send information from another file. Help me, please

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You could try something like this :

app.js

 var HOST = '0.0.0.0'
 var PORT = 6969;

 var server = net.createServer();
 server.listen(PORT, HOST);
 console.log('Server listening on ' + server.remoteAddress + ':' + 
 server.remotePort);

 const socket = require("./controllers/socket")(server);
 app.get("/socket/connection", socket.connection);

socket.js

const net = require('net');

module.exports = function(server) {

   module.connection = function(request, response) {

     server.on('connection', function(socket) {
       console.log('CONNECTED: ' + socket.remoteAddress + ':' + 
       socket.remotePort);
       socket.write('Hello from server');
       // other stuff is the same from here
       socket.on('data', function(data) {
          socket.write('reply data ' + data);
       });
     });


   return module;
};
over 4 years ago · Santiago Trujillo Relatório
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