Bind socket to specific IP when creating connections

2,353 views
Skip to first unread message

Maarten

unread,
Nov 16, 2011, 12:10:53 PM11/16/11
to nodejs
Hello,

I need to create connections in Node from a specific IP address on a
server with more than one IP address. Unfortunately, the Socket API
does not support binding to a local address when creating a
connection.

Does anyone know if there is a workaround for this? I already looked
at Node's sources, but the bind call is deeply hidden in the
libraries.

Thanks,

Maarten

Diogo Resende

unread,
Nov 16, 2011, 5:02:16 PM11/16/11
to nod...@googlegroups.com

You mean othern than 0.0.0.0 or 127.0.0.1? You mean this?

var http = require("http")
var server = http.createServer()

server.listen(80, "192.168.100.1") // or other?

This works for me..

---
Diogo R.

Thomas Shinnick

unread,
Nov 16, 2011, 5:17:48 PM11/16/11
to nod...@googlegroups.com
My problem, and perhaps Maarten's, is that I sometimes need to bind to a particular IP address while creating an _outgoing_ Socket connection.  I need to _originate_ a connection from a particular address.  Can't be done.

server.listen is the only API that does bind that I've found yet.  There is a conspicuous asymmetry in the networking area still, favoring simple HTTP/server-type incoming connections.  Not really surprising, that, but irksome nonetheless.

Oh, and I have further troubles selecting ipv4 vs. ipv6 for connections.  But I figure that might have to wait until the Mesozoic era.  At least we're into the Cambrian explosion, 7 legs 5 eyes milli-tubes and all...

Ben Noordhuis

unread,
Nov 16, 2011, 5:17:59 PM11/16/11
to nod...@googlegroups.com

It's not possible right now, it's one of those minor TODOs that no one
ever gets around to. If you want to submit a pull request,
Socket.prototype.connect in lib/net.js is the function you want.

Maarten

unread,
Nov 23, 2011, 3:35:17 AM11/23/11
to nodejs
I noticed that exports._createServerHandle in lib/net.js created a
generic socket handle and does a bind on it. Thus it can also be used
for client sockets.

In application code I created a function similar to createConnection
which does:

var s = new net.Socket({ handle:
net._createServerHandle(localAddress) });
s.connect(port, host, cb);

I am not comfortable enough with javascript and the node architecture
yet to submit a patch to net.js.

Thanks,

Maarten O.

On Nov 16, 11:17 pm, Ben Noordhuis <i...@bnoordhuis.nl> wrote:


> On Wed, Nov 16, 2011 at 18:10, Maarten <maar...@brightcode.nl> wrote:
> > Hello,
>
> > I need to create connections in Node from a specific IP address on a
> > server with more than one IP address. Unfortunately, theSocketAPI
> > does not support binding to a local address when creating a
> > connection.
>
> > Does anyone know if there is a workaround for this? I already looked

> > at Node's sources, but thebindcall is deeply hidden in the


> > libraries.
>
> > Thanks,
>
> > Maarten
>
> It's not possible right now, it's one of those minor TODOs that no one

> ever gets around to. If you want to submit a pull request,Socket.prototype.connect in lib/net.js is the function you want.

Bert Belder

unread,
Nov 30, 2011, 9:36:09 PM11/30/11
to nodejs
On Nov 16, 11:17 pm, Ben Noordhuis <i...@bnoordhuis.nl> wrote:

Ben,

I think we should have net.createConnection take an options object
first. Maybe extend Maciej's tls options patch to also cover the net
module? After that binding to a specific address should be trivial to
add (it's just adding an extra bind() call).

- Bert

Ben Noordhuis

unread,
Dec 1, 2011, 7:17:20 AM12/1/11
to nod...@googlegroups.com

Sure. Make it so.

Pyro Strex

unread,
Jan 2, 2012, 9:38:55 AM1/2/12
to nod...@googlegroups.com
I hope this discussion will soon be released. I really am trying to do the same thing, but I don't really like to hack nodejs.
Reply all
Reply to author
Forward
0 new messages