我在 node js 中编写了一个简单的服务器。
var tls = require("tls"), fs = require("fs"), sys = require("sys");
//Number of messages received
var received=0;
var options = {
key: fs.readFileSync("certs/keys/server.key"),//Server private key
cert:fs.readFileSync("certs/certs/server.crt"),//Server cert.
requestCert: true,//Require client to send it's certificate
rejectUnauthorized:true,
ca:fs.readFileSync("certs/certs/userA.crt") //Root certificate,
};
//Server instance with connection request callback
var server = tls.createServer(options,function(socket){
//Add a listener for receiving data packets
socket.addListener("data", function(data){
received++;
});
}).listen(2195,function(){
console.log("Server started");
});
我还有 java 客户端应用程序,它与服务器建立多个 (300) 连接并发送消息。问题是变量“received”的值与 java 端的“send”值不匹配。为了。例如,如果我从 java 应用程序发送 100,000 条消息,服务器显示收到的值为 80,000,即使服务器成功接收了所有消息。
我认为问题是收到的变量同时被多个回调更新,因此最终值变得混乱。关于如何解决这个问题的任何想法?