对node.js的net模块的一个测试
生活随笔
收集整理的這篇文章主要介紹了
对node.js的net模块的一个测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
抄表系統在接收電表發回的數據的時候,發現有些電表發回的數據沒有被socket端口接收到,最后等待時間耗盡,留下了指令超時的記錄。因為是在進行虛擬的測試,因此可以根據對服務器發的tcp報文進行分析,分析結果發現在發出抄表指令之后1s之內,虛擬的電表就返回了抄表結果,然后socket服務器沒有收到這個記錄。
因此做了這個測試,來測試抄表時間和抄表數量對于socket服務器有什么影響。
服務器端:
var net = require('net'); var fs = require('fs');var server = net.createServer(function (socket) {socket.on('data', function (data) {data = JSON.parse(data);// console.log(data.i + ' : ' + data.curr);var record = data.i + ' : ' + data.curr + '\r\n';fs.appendFile("./服務器日志.txt", record, function(err) {if (err) {console.log("指令記錄失敗:" + record);}});var time = new Date();var current = data.i + ' : ' + time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate() + " " + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() + ":" + time.getMilliseconds() + " 服務器端 ====>> 客戶端" ;socket.write(current);});socket.on('end',function () {console.log('連接斷開');});socket.on('error',function () {console.log('連接斷開');}) });server.listen(8814, function() {console.log('server start!'); });客戶端:
var net = require('net'); var fs = require('fs');var i = 1;var client = net.connect({port:8814}, function() {setInterval(emmit, 500);// client.end(); });function emmit() {if (i <= 4000) {console.log(i);var time = new Date();var current = time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate() + " " + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() + ":" + time.getMilliseconds() + " 客戶端 ====>> 服務器端" ;var data = {};data.curr = current;data.i = i;data = JSON.stringify(data);client.write(data);};i++; }client.on('data', function(data) {// console.log(data.toString());var record = data.toString() + '\r\n';fs.appendFile("./客戶端日志.txt", record, function(err) {if (err) {console.log("指令記錄失敗:" + record);}}); });client.on('end', function() {console.log('client disconnected'); });client.on('error', function() {console.log('server error!'); });?
轉載于:https://www.cnblogs.com/lswit/p/5016914.html
總結
以上是生活随笔為你收集整理的对node.js的net模块的一个测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: J2EE,J2SE,J2ME,JDK,S
- 下一篇: Orchard中如何配置远端发布