我正在尝试让客户端连续发送空字符串。我无法在第四次尝试时启动客户端。请参阅“//问题在这里开始”。
我收到的控制台消息
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 :(得分: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
}