Linux Platform: CentOS release 6.5 (Final)
Windows platform: Windows 7 Ultimate Edition
The server-side code is as follows:
varNET = require ('Net');varServer =Net.createserver (function (c) {Console.log ('Client Connected:'+c.remoteaddress); C.setnodelay (true); C.on ('Data', function (data) {Console.log (data); }); C.on ('End', function () {Console.log ('disconnected from client'); }); C.on ('Error', function (err) {Console.log (err); });}); Server.listen ({port:6200}, Function () {Console.log ('Server Bound');});
The client code is as follows:
Const NET = require (' net '); Const client = Net.connect ({port:6200, localport:6201}, function () { //' Connect ' listener Console.log (' connected to server! '); Client.setnodelay (true); For (Var i=0, i<1000; i++) { var now = Date.now (); while (Date.now () < now + +) {} client.write (' Chinese '); }}); Client.on (' Data ', (data) =>{ Console.log (data.tostring ());}); Client.on (' End ', () =>{ console.log (' Disconnected from server ');});
Running Server.js on Linux, then executing client.js on Linux and Winows, and pressing CTRL + C to terminate the process during execution, I found the server side to react differently. When the client is terminated on Linux, the server outputs "disconnected from client", and when the client is terminated on the window, the server outputs "{[Error:read Econnreset] Code: ' Econnreset ', errno: ' Econnreset ', syscall: ' Read '} (if the server side listens to the error event, the server side throws an exception and exits). Why is that? I used the tcpdump tool to listen to the server port 6200 port, found the reason.
When the CTRL + C key is pressed, the client on Linux sends a message with the fin tag to the server 16:53:59.948186 IP localhost.6201 > Localhost.lm-x: Flags [F], seq, ack 1 , win 257, options [Nop,nop,ts Val 1250056051 ECR 1250053568], length 0
When the CTRL + C key is pressed, the client on Windows sends a message with the RST token to the server 16:55:14.681005 IP localhost.6201 > Localhost.lm-x: Flags [R], seq 6007, ACK 1, win 0, length 0
Note:
1. Reset bit (RST): when rst=1 indicates a serious error in the TCP connection, the connection must be released and then re-established.
The different performance of "NodeJs" CTRL + C in TCP connection under Linux platform and Windows platform