4
4
<meta charset="utf-8">
5
<title>UDP / Datagram Sockets Node.js v0.10.21 Manual & Documentation</title>
5
<title>UDP / Datagram Sockets Node.js v0.10.22 Manual & Documentation</title>
6
6
<link rel="stylesheet" href="assets/style.css">
7
7
<link rel="stylesheet" href="assets/sh.css">
8
8
<link rel="canonical" href="http://nodejs.org/api/dgram.html">
149
149
<li><code>buf</code> Buffer object. Message to be sent</li>
150
150
<li><code>offset</code> Integer. Offset in the buffer where the message starts.</li>
151
151
<li><code>length</code> Integer. Number of bytes in the message.</li>
152
<li><code>port</code> Integer. destination port</li>
153
<li><code>address</code> String. destination IP</li>
154
<li><code>callback</code> Function. Callback when message is done being delivered.
152
<li><code>port</code> Integer. Destination port.</li>
153
<li><code>address</code> String. Destination hostname or IP address.</li>
154
<li><code>callback</code> Function. Called when the message has been sent. Optional.</li>
157
<p>For UDP sockets, the destination port and IP address must be specified. A string
158
may be supplied for the <code>address</code> parameter, and it will be resolved with DNS. An
159
optional callback may be specified to detect any DNS errors and when <code>buf</code> may be
160
re-used. Note that DNS lookups will delay the time that a send takes place, at
161
least until the next tick. The only way to know for sure that a send has taken place
162
is to use the callback.
165
<p>If the socket has not been previously bound with a call to <code>bind</code>, it's
166
assigned a random port number and bound to the "all interfaces" address
167
(0.0.0.0 for <code>udp4</code> sockets, ::0 for <code>udp6</code> sockets).
156
<p>For UDP sockets, the destination port and address must be specified. A string
157
may be supplied for the <code>address</code> parameter, and it will be resolved with DNS.
160
<p>If the address is omitted or is an empty string, <code>'0.0.0.0'</code> or <code>'::0'</code> is used
161
instead. Depending on the network configuration, those defaults may or may not
162
work; it's best to be explicit about the destination address.
165
<p>If the socket has not been previously bound with a call to <code>bind</code>, it gets
166
assigned a random port number and is bound to the "all interfaces" address
167
(<code>'0.0.0.0'</code> for <code>udp4</code> sockets, <code>'::0'</code> for <code>udp6</code> sockets.)
170
<p>An optional callback may be specified to detect DNS errors or for determining
171
when it's safe to reuse the <code>buf</code> object. Note that DNS lookups delay the time
172
to send for at least one tick. The only way to know for sure that the datagram
173
has been sent is by using a callback.
170
176
<p>Example of sending a UDP packet to a random port on <code>localhost</code>;
365
371
<li><a href="http://twitter.com/nodejs" class="twitter">@nodejs</a></li>
368
<p>Copyright <a href="http://joyent.com/">Joyent, Inc</a>, Node.js is a <a href="/trademark-policy.pdf">trademark</a> of Joyent, Inc. View <a href="https://raw.github.com/joyent/node/v0.10.21/LICENSE">license</a>.</p>
374
<p>Copyright <a href="http://joyent.com/">Joyent, Inc</a>, Node.js is a <a href="/trademark-policy.pdf">trademark</a> of Joyent, Inc. View <a href="https://raw.github.com/joyent/node/v0.10.22/LICENSE">license</a>.</p>
371
377
<script src="../sh_main.js"></script>