Node socket error, what is the reason? In addition, I want to set the connection timeout, how to write it?

  node.js, question



1.client.connetcThe second parameter 43 represents the What? ? ? ? , client.connect receives only two parameters (the second option)socket.connect(options[, connectListener])

client.connect(server, function(){
 console.log('connected successfully');

I want to know yours.serverWhat value can be passed in{ port: 8888, host: 'localhost' }Or ..{ path: '/xxx/tt.sock'}

3. Set the timeout, directly set the timeout, see the following code in detail, but you should be clear, even if the timeout, just set off a.timeoutThe socket connection of the event, socket will not be closed, and must be closed manually (call end (), or destory ()).

4. I didn’t see the code of your server and the code called by the client. I wrote an example for you to look at.

Considering your version, I will try my best to use ES5

var net = require('net')
 var server = net.createServer(function(socket) {
 }).on('error', function(err) {
 // handle errors here
 throw err
 // listen on localhost:8888.
 host: 'localhost',
 port: 8888
 }, function() {
 console.log('opened server on', server.address())


var net = require('net')
 var output = ''
 var client = new net.Socket();
 port: 8888,
 host: 'localhost'
 }, function() {
 console.log('connetc to server successfully')
 //Set timeout method
 Client.setTimeout(3000) ///Set 3s Timeout
 client.on('timeout', function() {
 console.log('timeout for client')
 client.on('data', function(data) {
 output = data.toString()