我的情况如下:有一个IP地址数组。我将测试每个 IP 以连接到远程服务器。如果一个 IP 连接,其余 IP 将被忽略并且不会连接。
我使用以下 Node.JS 代码来完成这项工作,但它似乎不起作用。请给出一些提示。谢谢!
// serverip is a var of string splitted by ";", e.g. "ip1;ip2;ip3"
var aryServerIP = serverip.split(";");
console.log(aryServerIP);
var ipcnt = aryServerIP.length; // ipcnt = 3, for example
for (ip in aryServerIP)
{
console.log("to process: " + ipcnt); // error here: always print 3
var net = require('net');
var client = new net.Socket();
var rdpport = 3389;
client.connect(rdpport, aryServerIP[ip], function(){
console.log("socket connected to " + aryServerIP[ip] + ":" + rdpport);
client.destroy();
if (0 != ipcnt)
{
// do some real connection work about aryServerIP[ip].
ipcnt--;
}
});
client.on('error', function(){
console.log("fail to connect to " + aryServerIP[ip] + ":" + rdpport);
ipcnt--;
});
}
我知道使用 ipcnt count 来控制循环是不好的,但是当循环中调用了异步函数时,我不知道如何控制 Node.JS 循环......