localPort of a socket - possible bug in Node?

52 views
Skip to first unread message

Phani Kumar

unread,
Jul 2, 2015, 9:31:20 AM7/2/15
to nod...@googlegroups.com
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.


Ryan Graham

unread,
Jul 2, 2015, 10:42:24 PM7/2/15
to nod...@googlegroups.com

On Thu, 2 Jul 2015 at 14:17 Ryan Graham <r.m.g...@gmail.com> wrote:
Looks like a bug, and I think I might even have a fix for it.. I'll submit a PR shortly, would you mind reporting the issue?


~Ryan

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/cff4c089-b632-4344-a5e8-7af45b41ef38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ryan Graham

unread,
Jul 2, 2015, 10:42:24 PM7/2/15
to nod...@googlegroups.com
Looks like a bug, and I think I might even have a fix for it.. I'll submit a PR shortly, would you mind reporting the issue?

~Ryan

On Thu, 2 Jul 2015 at 06:31 Phani Kumar <pha...@gmail.com> wrote:

Phani Kumar

unread,
Jul 3, 2015, 9:52:50 AM7/3/15
to nod...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages