我在node.js中遇到TCP服务器问题。有时我必须将ubuntu时钟与主服务器时间同步。时间正在改变,但我的应用程序出现问题。 3-5个日期更改后,服务器会写入一些错误:
{ Error: socket hang up
at createHangUpError (_http_client.js:302:15)
at Socket.socketOnEnd (_http_client.js:394:23)
at emitNone (events.js:91:20)
at Socket.emit (events.js:188:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9) code: 'ECONNRESET' }
我找不到任何类似的问题,有人会更改日期。 我的想法是在每次更改日期时重新启动TCP Server,但它没有帮助。
TCP服务器代码:
var net = require('net');
var TCPCommands = require.main.require('./src/TCP/TCPCommands');
var config = require.main.require('./config');
var server = net.createServer(function (socket) {
var remoteAddress = socket.remoteAddress + ':' + socket.remotePort;
socket.on('data', function (data) {
console.log("Incoming buffer: ");
console.log(data);
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {console.log(stdout)}
exec("date -s " + "'" + moment("SAMPLE DATE").format(localFormat) + "'", puts);
exec("hwclock --systohc --localtime", puts);
module.exports.restartTCPServer();
socket.write("responseBuffer");
});
socket.once('close', function() {
console.log("Connection from " + remoteAddress + " closed");
socket.end();
});
socket.on('error', function(err) {
console.error("Connection " + remoteAddress);
console.error(err.message);
socket.destroy();
});
});
module.exports = {
startTCPServer: function() {
server.listen(config.pttTcpServerPort, function () {
console.log("TCP Server is running " + config.pttIpAddress + ":" + config.pttTcpServerPort);
});
},
stopTCPServer: function() {
server.close(function () {
console.log("Stopping TCP Server");
});
},
restartTCPServer: function() {
var t = this;
console.log("Restarting TCP Server");
t.stopTCPServer();
t.startTCPServer();
}
}