A test of the net module for node. js

Source: Internet
Author: User

The meter reading system when receiving the data sent back by the meter, found that some meter sent back data is not received by the socket port, the last waiting time is exhausted, leaving a record of the instruction timeout. Because it is in the virtual test, it can be based on the TCP messages sent to the server analysis, the analysis results found that after the issuance of reading instructions within 1s, the virtual meter returned the meter reading results, and then the socket server did not receive this record.

Therefore, this test is done to test how the meter reading time and the number of meter reading affect the socket server.

Server-side:

varNET = require (' net ');varFS = require (' FS ');varServer = Net.createserver (function(socket) {Socket.on (' Data ',function(data) {data=json.parse (data); //Console.log (data.i + ': ' + data.curr);        varRecord = Data.i + ': ' + Data.curr + ' \ r \ n '; Fs.appendfile ("./server log. txt", record,function(err) {if(Err) {Console.log ("Instruction Record failed:" +record);        }        }); varTime =NewDate (); varCurrent = Data.i + ': ' + time.getfullyear () + "-" + (Time.getmonth () + 1) + "-" + time.getdate () + "" + time.gethours () + ":" + time.getminutes () + ":" + time.getseconds () + ":" + time.getmilliseconds () + "server-side ====>> client" ;    Socket.write (current);    }); Socket.on (' End ',function() {Console.log (' Disconnected ');    }); Socket.on (' Error ',function() {Console.log (' Disconnected '); }); Server.listen (8814,function() {Console.log (' Server start! ');});

Client:

varNET = require (' net ');varFS = require (' FS ');vari = 1;varClient = Net.connect ({port:8814},function() {setinterval (Emmit,500); //client.end ();    });functionEmmit () {if(I <= 4000) {console.log (i); varTime =NewDate (); varCurrent = Time.getfullyear () + "-" + (Time.getmonth () + 1) + "-" + time.getdate () + "" + time.gethours () + ":" + time.ge Tminutes () + ":" + time.getseconds () + ":" + time.getmilliseconds () + "Client ====>> server Side" ; vardata = {}; Data.curr=Current ; DATA.I=i; Data=json.stringify (data);    Client.write (data);    }; I++;} Client.on (' Data ',function(data) {//Console.log (data.tostring ());    varRecord = data.tostring () + ' \ r \ n '; Fs.appendfile ("./client log. txt", record,function(err) {if(Err) {Console.log ("Instruction Record failed:" +record); }    });}); Client.on (' End ',function() {Console.log (' Client Disconnected ');}); Client.on (' Error ',function() {Console.log (' Server error! ');});

A test of the net module for node. js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.