1
다음은 간단한 Node.js tcp 서버입니다. 연결 해제 후 소켓 객체 액세스
var net = require('net');
var conn = [];
var server = net.createServer(function(client) {//'connection' listener
var i = conn.push(client) - 1;
//console.log('[conn',i,']', client.remoteAddress, ':', client.remotePort);
console.log('[disc]', conn[i].remoteAddress, ':', conn[i].remotePort);
client.on('end', function() {
console.log('[disc]', conn[i].remoteAddress, ':', conn[i].remotePort);
});
client.on('data', function(msg) {
console.log('[data]', msg.toString());
})
client.write('hello\r\n');
//client.pipe(client);
});
server.listen(8080);
클라이언트가 연결
는 보내거나 연결을 끊습니다, 그것은 클라이언트에 대한 정보를 인쇄합니다. 그러나 클라이언트가 연결을 끊을 때만 소켓 정보가 아닌undefined
이 인쇄됩니다. 예 :
[conn] 127.0.0.1 : 52711
[data] 127.0.0.1 : 52711 world!
[disc] undefined : undefined
왜 이런 일이 발생합니까? 소켓을 닫을 때 소켓이 파괴 되었기 때문입니까? 닫는 소켓에 관한 정보를 알아야합니다.