关于net模块下的socket,接收数据存储数据库问题,请高手指点,数据接收不全,对node还是没有很好理解,我觉得是处理回调函数时的问题,刚上手,不太清楚,贴代码!
发布于 12 年前 作者 talentfs 5438 次浏览 最后一次编辑是 8 年前

一个小程序,用来接收一些数字信息,并写入数据库。 var net = require(‘net’); var sql = require(‘msnodesql’); var useTrustedConnection = true; var conn_str = “Driver={” + driver + “};Server=” + server + “;” + (useTrustedConnection == false ? “Trusted_Connection={Yes};” : “UID=” + user + “;PWD=” + pwd + “;”) + “Database={” + database + “};”; var Time = new Array();
var Ip=new Array(); var index1 = 0; var index2 = 0; var index3 = 0;//接受数据条数 var flag = true; var length = 12 *100;//每条数据长度为12个字节 buf1 = new Buffer(length); buf2 = new Buffer(length);

net.createServer(function(sock) { sock.setEncoding(‘binary’); // 我们获得一个连接 - 该连接自动关联一个socket对象 console.log('CONNECTED: ’ + sock.remoteAddress + ‘:’ + sock.remotePort);

// 为这个socket实例添加一个"data"事件处理函数

sock.on('data', function(data) {

//判断buf1是否已满,满了就存入数据库,并修改个标志位 if ((flag == true) && (index1 == length)) { sql.open(conn_str, function(err, conn) { if (err) { console.log(“err:” + err); } else { for (var i = 0; i < index3; i++) { conn.queryRaw(“INSERT INTO RoanCondition(speed,number,No,StartTime,Ip) VALUES(” + buf1.readFloatLE(8 + i * 12) + “,” + buf1.readInt32LE(4 + i * 12) + “,” + buf1.readInt32LE(0 + i * 12) + “,’” + Time[i] + “’,’” + Ip[i] + “’)”, function(err, results) { if (err) { console.log(“data1 err:” + err); }
}); } flag = false; index3 = 0; index1 = 0; } });

}
//判断buf2是否已满,满了就存入数据库,并修改个标志位
    if ((flag == false) && (index2 == length)) {
        sql.open(conn_str, function(err, conn) {
            if (err) {
                console.log("err:" + err);
            }
            else {
                for (var i = 0; i < index3; i++) {
                    conn.queryRaw("INSERT INTO RoanCondition(speed,number,No,StartTime,Ip) VALUES(" + buf2.readFloatLE(8 + i * 12) + "," + buf2.readInt32LE(4 + i * 12) + "," + buf2.readInt32LE(0 + i * 12) + ",'" + Time[i] + "','" + Ip[i] + "')", function(err, results) {
                        if (err) {
                            console.log("data2 err:" + err);
                        }
                    });
                }
                flag = true;
                index2 = 0;
                index3 = 0;   
            }
        });          
    }
    //接受客户端数据       
    var len = data.length;
    //若buf1未满,则存入
    if ((flag == true) && (index1 < length)) {
        for (var i = 0; i < len; i++) {
            buf1[index1] = data.charCodeAt(i);
            index1++;
        }
      //记录时间和ip
        Ip[index3] = sock.remoteAddress;
        var Datetime = new Date();
        var time = Datetime.toLocaleTimeString();
        var year = Datetime.getFullYear();
        var mouth = Datetime.getMonth() + 1;
        var day = Datetime.getDate();
        Time[index3] = year + "-" + mouth + "-" + day + " " + time;

    }
    ///若buf2未满,则存入
    else if ((flag == false) && (index2 < length)) {
        for (var i = 0; i < len; i++) {
            buf2[index2] = data.charCodeAt(i);
            index2++;
        }
        Ip[index3] = sock.remoteAddress;
        var Datetime = new Date();
        var time = Datetime.toLocaleTimeString();
        var year = Datetime.getFullYear();
        var mouth = Datetime.getMonth() + 1;
        var day = Datetime.getDate();
        Time[index3] = year + "-" + mouth + "-" + day + " " + time;
    }
    index3++;
});

// 为这个socket实例添加一个"close"事件处理函数
sock.on('close', function(data) {
    console.log('CLOSED: ' +
        sock.remoteAddress + ' ' + sock.remotePort);
});
//存储间隔时间

}).listen(PORT, HOST);

console.log('Server listening on ’ + HOST + ‘:’ + PORT);

1 回复

只会动态语言的标记对手动操作 Buffer 好陌生…

回到顶部