2017-10-02 8 views
1

서버 :Node.js가 Client에 Server 입력을 보냅니다. 메모리 누수에 대한 경고. 내 실수는 무엇입니까? 그리고 node.js는 어떻게 코드를 실행합니까?

var net = require('net'); 
var stdin = process.openStdin(); 

var client_list = []; 

var server = net.createServer(function(connection) { 
    //console.log('client connected'); 

    connection.on('error', function(e){ 
     if(e.code == 'ECONNRESET'){ 
     console.log('Client dissconeccted'); 
     } 
    }); 
    //connection.write('Hello World!\r\n'); 
    stdin.addListener("data", function(d) { 
    // note: d is an object, and when converted to a string it will 
    // end with a linefeed. so we (rather crudely) account for that 
    // with toString() and then trim() 

    console.log("you entered: [" + d.toString().trim() + "]"); 
     connection.write(d.toString().trim()); 
    }); 
    connection.pipe(connection); 

}); 
server.listen(9999, function() { 
    console.log('server is listening'); 
}); 

클라이언트 (?) :

"Possible EventEmitter memory leak detected. 11 data listeners added. Use emitter.setMaxListeners() to increase limit".

var net = require('net'); 
var HOST = 'localhost'; 
var PORT = 9999; 
//var client = new net.Socket(); 

    var client = net.connect(PORT, HOST, function(){ 
     console.log('connected to server! ' + HOST + ':' + PORT); 
     //client.write('I am Superman'); 
    }); 

    client.on('data', function(data) { 
     var data = data.toString(); 
     console.log(data); 

     //If data starts with JS add injection functionallity 
     if (data === "END"){ 
      client.end(); 
      console.log("ENDING!") 
     } 
     else if (data === "poo"){ 
      console.log("HOLY SHIT!") 
     } 
    }); 

    //Keep trying to connect! 
    client.on('error', function(e) { 
     console.log('Parent connection error'); 
     //client.end(); 
     client.connect(PORT, HOST); 
    }); 

    client.on('end', function() { 
     console.log('disconnected from server'); 
    }); 

/*var client = net.connect({port: 8080}, function() { 
    console.log('connected to server!'); 
});*/ 

그래서 무슨 일이 추가 청취자를 유지하고 메시지와 함께 11 청취자 날을 경고이다 이거 야? stdin.addListener()을 움직여 문제를 해결하려고했지만 입력을 전혀받지 않거나 문제가 지속됩니다. 내가 뭔가? 노드에서 코드가 실행되는 방식은 무엇입니까?

미리 감사드립니다.

답변

0

클라이언트 및 서버 스크립트를 모두 실행하십시오. 오류 메시지가 나타납니다. 나는 Ubuntu에서 코드를 실행하고있다. nodejs 6.9.5, npm 5.4.2. package.json 파일의 내용을 게시 할 수 있습니까?

업데이트 : 온라인 상태였습니다. 노드의 알려진 오래된 버그처럼 보입니다. https://github.com/nodejs/node-v0.x-archive/issues/5108

+0

{ "이름": "TCP", "버전": "1.0.0", "설명": "TCP 클라이언트/서버 테스트", "주": "server.js" "스크립트": { "test": "echo \"오류 : 테스트를 지정하지 않았습니다. \ "&& exit 1" , "author": "Juliet", "라이센스": "ISC", "dependencies" { "net": "^ 1.0.2" } } –