TooTallNate / node-nat-pmp

Node.js implementation of the NAT Port Mapping Protocol
132 stars 16 forks source link

Error: Socket is already bound #8

Open jczimm opened 10 years ago

jczimm commented 10 years ago

I'm using nat-pmp for my server.

var natpmp = require('nat-pmp');
...

var client = natpmp.connect("192.168.1.1");

client.portMapping({ private: 8070, public: 8070, ttl: 0 }, function (err, info) {
  if (err) throw err;
      console.log(info);

      // server code
      ...

      client.close();
});

On Windows I get this error:

$ node main.js
dgram.js:159
    throw new Error('Socket is already bound');
          ^
Error: Socket is already bound
    at Socket.bind (dgram.js:159:11)
    at Client.connect (C:\Users\jczimm\uSocketConsole\node_modules\nat-pmp\index.js:93:15)
    at C:\Users\jczimm\uSocketConsole\node_modules\nat-pmp\index.js:57:12
    at process._tickCallback (node.js:343:11)
    at Function.Module.runMain (module.js:492:11)
    at startup (node.js:124:16)
    at node.js:807:3

Does nat-pmp simply not work on Windows? Thanks!

ghost commented 10 years ago

Having same issue and nat-pmp has been the most promising module, all the others have failed or weren't for windows, am about to just give up, just some things not possible yet with node.js!

Is this project like most of the others dead?

pose commented 9 years ago

@jczimm @werescape This worked for me:

var natpmp = require('nat-pmp');
...

var client = natpmp.connect("192.168.1.1");

setTimeout(function () {
  client.portMapping({ private: 8070, public: 8070, ttl: 0 }, function (err, info) {
    if (err) throw err;
        console.log(info);

        // server code
      ...

        client.close();
  });
});

As the this.socket.bind(port) is executed twice (because it's queued till next setTimeout) by changing the code connect is only executed once. Probably, this can be patched so it never happens but it's a good workaround for now. Hope that helps!

Tillman32 commented 9 years ago

I'm having this issue too, I can't seem to get it to work. I tried @pose's solution, with no luck.

dgram.js:161 - Socket Is already bound

Vykook commented 9 years ago

Same problem on OS X.

KritR commented 9 years ago

I found the solution. You need to change the following exports.CLIENT_PORT = 5352 in node-nat-pmp/Index.js to another port that's open.

There's various ports that work. I set it to 0 currently as that makes it binds to any random kernel selected port.

In addition, do not use the code from the npm install. That code is outdated. Use the index.js from this git repo.

Tillman32 commented 9 years ago

@SkeletonSlayerz Awesome, thank you! I was able to get it working using 0 as well.

KritR commented 9 years ago

@Tillman32 Yep. Only problem with this is that it doesn't conform to the NAT-PMP standard, but it should be fine if you're only opening the port for a short amount of time.

I found Bonjour's mDNS to be of the offending nature in this case, taking up the 5352 Port on Mac. It doesn't set SO_REUSEADDR by default and therefore it turns into the only service that can bind to the port.

mikeal commented 8 years ago

Any fix for this?