2014-02-23 47 views
0

我試圖讓一個客戶端開始連續發送空字符串。我無法讓客戶在第四次嘗試時開始。請參閱「//從這裏開始的問題」。nodejs不踢無禮

控制檯消息我得到

Removed newline: 
consecutiveWarningCount: 0 
Removed newline: 
consecutiveWarningCount: 1 
Removed newline: 
consecutiveWarningCount: 2 
Removed newline: 
consecutiveWarningCount: 3 
Removed newline: 
consecutiveWarningCount: 4 
Removed newline: 
consecutiveWarningCount: 5 

守則

//Load the tcp library 
var net = require('net'); 

//Keep track of connected clients 
var clients = []; 

// Vars 
var ADDRESS = '127.0.0.1'; 
var PORT = 3001; 
var consecutiveWarningCount = 0; 

//Start TCP Server 
var s = net.createServer(function(cl) { 
    //console.log('Server connected'); 
    cl.setEncoding('utf8'); 
    cl.name = cl.remoteAddress + ":" + cl.remotePort; 

    //Put the client into the list 
    clients.push(cl); 
    console.log('Pushed ' + cl.name + ' into client array'); 

    cl.on('end', function() { 
    clients.splice(clients.indexOf(cl), 1); 
    console.log('Server disconnected'); 
    }); 

    cl.on('data', function(data) { 
    data = removeCarriageReturns(data);  //PROBLEM STARTS HERE. 
    if (data.length == 0) { 
     cl.write("You sent an empty string\r\n"); 
     consecutiveWarningCount += 1; 
     console.log('consecutiveWarningCount: ' + consecutiveWarningCount); 
    } 
    else if ((data.length == 0) && (consecutiveWarningCount == 1)) { 
     cl.write("You sent an empty string again. This is your first warning."); 
     consecutiveWarningCount += 1; 
     console.log('consecutiveWarningCount: ' + consecutiveWarningCount); 
    } 
    else if ((data.length == 0) && (consecutiveWarningCount == 2)) { 
     cl.write("You sent an empty string again. This is your second warning."); 
     consecutiveWarningCount += 1; 
     console.log('consecutiveWarningCount: ' + consecutiveWarningCount); 
    } 
    else if ((data.length == 0) && (consecutiveWarningCount == 3)) { 
     cl.write("You sent an empty string again. This is your last warning."); 
     cl.write("You will be disconnected if you send one more empty string"); 
     consecutiveWarningCount += 1; 
     console.log('consecutiveWarningCount: ' + consecutiveWarningCount); 
    } 
    else if ((data.length == 0) && (consecutiveWarningCount == 4)) { 
     cl.destroy(); 
     console.log('Kicked ' + cl.remoteAddress); 
    } 
    else { 
     console.log('Server received "' + data + '" from ' + cl.remoteAddress + ':' + cl.remotePort); 
     cl.write('Client wrote ' + data); 
     if (consecutiveWarningCount > 0) { 
     consecutiveWarningCount -= 1; 
     } 
    } 
    }); 

}); //End server 

s.listen(PORT, function() { //'listening' listener 

    //Start up information about the server 
    console.log('-----------------------------'); 
    console.log('Server bound to port: ' + PORT); 
    console.log('-----------------------------'); 
    console.log('Server listening...'); 
}); 

// Custom functions 
var removeCarriageReturns = function(str) { 
    var regexp = /\r\n/g; 
    var regexp_carriagereturn = /\r/g; 
    var regexp_newline = /\n/g; 

    if (str.charAt(str.length-1) == "\n") { 
    str = str.substring(0, str.length-2); 
    console.log("Removed newline: " + str); 
    if (str.charAt(str.length-1) == "\r") { 
     str = str.substring(0, str.length-2); 
     console.log("Removed carriage return:" + str); 
    } 
    } 
    return str; 
} 
+0

儘量不要用正則表達式重新發明輪子,而是對收到的數據使用限制,例如最大和最小長度的消息,也嘗試一些驗證和消毒模塊,刪除它不是安全的方式。 – Gntem

+0

嘿GeoPhoenix,我有正則表達式,但我沒有使用它們。 – Poliquin

回答

0

看看代碼(假設data.length等於零):

if (data.length == 0) { 
    //fire always 
} 
else if ((data.length == 0) && (consecutiveWarningCount == 1)) { 
    //never fired because you write "else" which means data.length != 0 
} 

嘗試重寫代碼這樣:

if (data.length == 0) { 
    if (consecutiveWarningCount == 0) { 
    //You sent an empty string 
    } else if (consecutiveWarningCount == 1) { 
    //You sent an empty string again. This is your first warning 
    } //... and so on 
} 
+0

嗨,祖布,謝謝。我有兩個併發的telnet連接。我觸發了一個被摧毀的事件,但事實是兩個人都被殺死了。有任何想法嗎? – Poliquin

+0

移動'var consecutiveWarningCount = 0;'到'var s = net.createServer(function(cl)....'函數,它現在似乎工作。 – Poliquin

+0

是的。我已經開始寫如何解決這個問題的大型答案,使用客戶端的IP,但它做得更容易,我想:) – Curious