NodeJs TCP服务器不一致地处理请求

时间:2017-05-18 13:56:05

标签: node.js http tcp connection

我正在尝试实现TCP服务器。但它不再随意接受请求。

我有时会遇到以下错误,有时我会找到正在发送的正确对象。

可能导致这种情况的原因是什么?

浏览器错误:

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.

浏览器成功:(服务器中的 config.tubes 对象)

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

服务器代码

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}`);

1 个答案:

答案 0 :(得分:0)

socket.destroy();

应该是

socket.end();