大家讲道理2017-04-17 14:53:56
1.client.connetc
What does the second parameter 43 mean? ? ? ? , client.connect only receives two parameters (the second one is optional) socket.connect(options[, connectListener])
client.connect(server, function(){
console.log('connected successfully');
})
2. I want to know what value your server
is passed in. It can be { port: 8888, host: 'localhost' }
or { path: '/xxx/tt.sock'}
3. To set the timeout, just set the timeout directly. See the code below for details, but you must be aware that even if it times out, only an timeout
event will be initiated, and the socket connection will not be closed and must be closed manually ( Call end(), or destroy()).
4. I didn’t see your server code or the code called by the client. I wrote an example for you to see for yourself
Considering your version issue, I try to use ES5 writing method
server.js
var net = require('net')
var server = net.createServer(function(socket) {
socket.end('goodbye\n')
}).on('error', function(err) {
// handle errors here
throw err
});
// listen on localhost:8888.
server.listen({
host: 'localhost',
port: 8888
}, function() {
console.log('opened server on', server.address())
});
client.js
var net = require('net')
var output = ''
var client = new net.Socket();
client.connect({
port: 8888,
host: 'localhost'
}, function() {
console.log('connetc to server successfully')
})
//设置超时方法
client.setTimeout(3000) //设置3s超时
client.on('timeout', function() {
console.log('timeout for client')
})
client.on('data', function(data) {
output = data.toString()
client.end();
})