简体   繁体   中英

NodeJs TCP server not handling requests consistently

I am trying to implement a TCP server. But it stops taking requests randomly.

I sometimes get following error and I sometimes get the right object that I am sending.

What might cause this?

Browser error:

The connection was reset

The connection to the server was reset while the page was loading.

    The site could be temporarily unavailable or too busy. Try again in a few moments.
    If you are unable to load any pages, check your computer’s network connection.
    If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

Browser success: (which is the config.tubes object in the server)

{"A":{"key":1,"status":2},"B":{"key":2,"status":0},"C":{"key":3,"status":0}}

Server code

net = require('net');
var server = net.createServer();

PORT = 5000;
ADDRESS = '127.0.0.1';

var server = net.createServer(onClientConnected);
server.listen(PORT);

function onClientConnected(socket) {
  console.log(`New client: ${socket.remoteAddress}:${socket.remotePort}`);
  socket.write(JSON.stringify(config.tubes));
  socket.destroy();
}

console.log(`Server started at: ${ADDRESS}:${PORT}`);
socket.destroy();

应该

socket.end();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM