여러개의 FTP Get을 하면 Error 메세지(Unable to make data connection )가 발생합니다.

조회수 1938회

Node.js의 ftp module을 통해서 동시에 여러개의 file을 다운 받는 것을 구현하려 합니다. 잘 될때도 있고 안될때도 있는데..

디버깅 해본 결과 1개의 파일 Get이 끝나기 전에 다음 파일의 Get을 시작하면 Unable to make data connection 메세지가 발생하면서 FTP가 끊깁니다.

뭔가 Sync를 맞춰서 해야할 것 같은데.. 조언 부탁드립니다.

var c = new Client();
c.on('ready', function() {
  c.list(remote_path, function(err, list) {
    if (err) throw err;
    for(var i=0; i<list.length; i++){
      if(list[i].type === 'd'){
        if (list[i].name != '.' && list[i].name != '..'){
          if(list[i].name.search(search_pattern) != -1){
            folders.push(list[i].name);
          }
        }
      }
    }
    console.log(folders.length + ' folders'); //: ' +  folders);
    for(let i=0; i<folders.length; i++){ // using 'let' for closure problem
      fs.mkdir(local_path+'/'+folders[i], function(err){

      });
      c.list(remote_path+'/'+folders[i], function(err, list) {
        NumFiles += list.length;
        list.forEach(function (element, index, array) {
            c.get(remote_path+'/'+folders[i]+'/'+element.name, function (err, stream) {
              stream.once('close', function () { c.end(); console.log('Connection Ended.'); });
              stream.pipe(fs.createWriteStream(local_path+'/'+folders[i]+'/'+element.name));
              LoadedFiles++;
              console.log('Progress : '+LoadedFiles+'/'+NumFiles);
            });
        });
      });
    }
  });
});
  • (•́ ✖ •̀)
    알 수 없는 사용자
  • get하는건 동기식인데 node.js 함수는 비동기식이라서 그런거 아닐까요? 동기식 프로그래밍이 가능한 라이브러리를 사용해보는것도 괜찮을것 같아요 알 수 없는 사용자 2016.8.19 19:10
  • 말씀하신 내용이 맞아요. 그래서 뭔가 강제로 동기를 맞춰서 1개의 Get이 끝나기 전에 다음 Get 시작을 못하게 막아야 하는데.. 어렵네요; 알 수 없는 사용자 2016.8.22 13:09

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)