我有一个简单的 TCP 服务器,如下所示:
var net = require('net');
var PORT = 6346;
var server = net.createServer(function(socket) {
socket.on('connect', function() {
console.log('Client connected: ' + socket.remoteAddress);
socket.write('Welcome to the server!\r\n');
});
socket.on('end', function() {
console.log('Client disconnected: ' + socket.remoteAddress);
});
socket.on('error', function() {
console.log('Client error: ' + socket.remoteAddress);
});
socket.on('timeout', function() {
console.log('Client timed out:' + socket.remoteAddress);
});
}).listen(PORT);
当我连接到服务器时,我看到了预期Client connected: 127.0.0.1
,但是当我断开连接时,我看到了Client disconnected: undefined
. 为什么 socket.remoteAddress 未定义,如何在断开连接时记录客户端的 IP?