Same codes, different results in REPL and shell environments about UDP socket

55 views
Skip to first unread message

mailto1587

unread,
Jun 23, 2014, 6:49:35 PM6/23/14
to nod...@googlegroups.com
It was a absolutely strange problem, the codes is quite simple:

```
~  cat main.js 
var dgram = require('dgram'),
    sock = dgram.createSocket('udp4');
sock.bind();
sock.setBroadcast(true);
```

And I first executed in shell environment with `node` command:

```
~ node main.js 

dgram.js:348
    throw errnoException(process._errno, 'setBroadcast');
          ^
Error: setBroadcast EBADF
    at errnoException (dgram.js:457:11)
    at Socket.setBroadcast (dgram.js:348:11)
    at Object.<anonymous> (/Users/mailto1587/main.js:5:6)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3
```

An exception raised, but what makes me surprised is that the same codes in REPL environment was successfully executed:

```
~  node
> var dgram = require('dgram'),
... sock = dgram.createSocket('udp4');
undefined
> sock.bind();
undefined
> sock.setBroadcast(true);
undefined
```

An abnormal issue, help please!

-- 
mailto1587
Sent with Airmail

mscdex

unread,
Jun 23, 2014, 9:50:55 PM6/23/14
to nod...@googlegroups.com
On Monday, June 23, 2014 6:49:35 PM UTC-4, Xiadong Zhu wrote:

An abnormal issue, help please!


It's most likely a timing issue. The non-REPL code is running within the same tick, so the socket isn't actually bound until some time later, but you try and use a method (setBroadcast) that expects a bound socket. The REPL code works because there is a long enough delay between the `sock.bind()` and `sock.setBroadcast()` such that the socket is bound by the time you press enter after typing `sock.setBroadcast()`.

The solution is to wait for the 'listening' event, either explicitly with `sock.on('listening', ...);` or by passing a callback to `sock.bind();` and calling your `sock.setBroadcast()`, etc. inside that callback/event handler.

mailto1587

unread,
Jun 27, 2014, 1:56:32 AM6/27/14
to nod...@googlegroups.com
I got it! Thank you very much!

-- 
mailto1587
Sent with Airmail

开启 2014年6月24日 at 下午5:06:02, mscdex (msc...@gmail.com) 写:

--
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/61708b7c-7ad9-4326-9d80-38b841f02f46%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages