I am using 0.10.31 node version. I am facing an issue which looks like a bug in node. Just wanted to confirm, before filing a bug and work on patch.
Here is the issue:
When the same socket object is used for multiple .connect(), the socket.localPort is not refreshing with new localPort used by the OS.
Test code:
---------------
In the below code both local port -1 and local port -2 prints the same local port, but in fact it is using different localPort (when checked by netstat or lsof).
var net = require('net');
var HOST = '200.xx.xx.204';
var PORT = 7777;
var client = new net.Socket();
client.on('close', function() {
console.log('Connection closed');
//on close just connect again on the same socket object
setTimeout (function() {
connectAgain();
},
5000);
});
client.on('error', function(error) {
console.log(error);
});
client.connect(PORT, HOST, function() {
console.log('local port - 1:' + client.localPort);
// on connect, close the socket. Close socket event will connect again using the same socket object
client.destroy(); //same with end also
});
function connectAgain() {
client.connect(PORT, HOST, function() {
console.log('local port - 2:' + client.localPort);
console.log('connected to ' + HOST + ':' + PORT);
});
}
Please let me know if this is a bug or expected.