{ "source": "doc/api/dgram.markdown", "modules": [ { "textRaw": "UDP / Datagram Sockets", "name": "dgram", "stability": 3, "stabilityText": "Stable", "desc": "

Datagram sockets are available through require('dgram').\n\n

\n

Important note: the behavior of dgram.Socket#bind() has changed in v0.10\nand is always asynchronous now. If you have code that looks like this:\n\n

\n
var s = dgram.createSocket('udp4');\ns.bind(1234);\ns.addMembership('224.0.0.114');
\n

You have to change it to this:\n\n

\n
var s = dgram.createSocket('udp4');\ns.bind(1234, function() {\n  s.addMembership('224.0.0.114');\n});
\n", "methods": [ { "textRaw": "dgram.createSocket(type[, callback])", "type": "method", "name": "createSocket", "signatures": [ { "return": { "textRaw": "Returns: Socket object ", "name": "return", "desc": "Socket object" }, "params": [ { "textRaw": "`type` String. Either 'udp4' or 'udp6' ", "name": "type", "desc": "String. Either 'udp4' or 'udp6'" }, { "textRaw": "`callback` Function. Attached as a listener to `message` events. Optional ", "name": "callback", "optional": true, "desc": "Function. Attached as a listener to `message` events." } ] }, { "params": [ { "name": "type" }, { "name": "callback", "optional": true } ] } ], "desc": "

Creates a datagram Socket of the specified types. Valid types are udp4\nand udp6.\n\n

\n

Takes an optional callback which is added as a listener for message events.\n\n

\n

Call socket.bind() if you want to receive datagrams. socket.bind() will\nbind to the "all interfaces" address on a random port (it does the right thing\nfor both udp4 and udp6 sockets). You can then retrieve the address and port\nwith socket.address().address and socket.address().port.\n\n

\n" }, { "textRaw": "dgram.createSocket(options[, callback])", "type": "method", "name": "createSocket", "signatures": [ { "return": { "textRaw": "Returns: Socket object ", "name": "return", "desc": "Socket object" }, "params": [ { "textRaw": "`options` Object ", "name": "options", "desc": "Object" }, { "textRaw": "`callback` Function. Attached as a listener to `message` events. ", "name": "callback", "desc": "Function. Attached as a listener to `message` events.", "optional": true } ] }, { "params": [ { "name": "options" }, { "name": "callback", "optional": true } ] } ], "desc": "

The options object should contain a type field of either udp4 or udp6\nand an optional boolean reuseAddr field.\n\n

\n

When reuseAddr is true socket.bind() will reuse the address, even if\nanother process has already bound a socket on it. reuseAddr defaults to\nfalse.\n\n

\n

Takes an optional callback which is added as a listener for message events.\n\n

\n

Call socket.bind() if you want to receive datagrams. socket.bind() will\nbind to the "all interfaces" address on a random port (it does the right thing\nfor both udp4 and udp6 sockets). You can then retrieve the address and port\nwith socket.address().address and socket.address().port.\n\n

\n" } ], "classes": [ { "textRaw": "Class: dgram.Socket", "type": "class", "name": "dgram.Socket", "desc": "

The dgram Socket class encapsulates the datagram functionality. It\nshould be created via dgram.createSocket(...)\n\n

\n", "events": [ { "textRaw": "Event: 'message'", "type": "event", "name": "message", "params": [], "desc": "

Emitted when a new datagram is available on a socket. msg is a Buffer and\nrinfo is an object with the sender's address information:\n\n

\n
socket.on('message', function(msg, rinfo) {\n  console.log('Received %d bytes from %s:%d\\n',\n              msg.length, rinfo.address, rinfo.port);\n});
\n" }, { "textRaw": "Event: 'listening'", "type": "event", "name": "listening", "desc": "

Emitted when a socket starts listening for datagrams. This happens as soon as UDP sockets\nare created.\n\n

\n", "params": [] }, { "textRaw": "Event: 'close'", "type": "event", "name": "close", "desc": "

Emitted when a socket is closed with close(). No new message events will be emitted\non this socket.\n\n

\n", "params": [] }, { "textRaw": "Event: 'error'", "type": "event", "name": "error", "params": [], "desc": "

Emitted when an error occurs.\n\n

\n" } ], "methods": [ { "textRaw": "socket.send(buf, offset, length, port, address[, callback])", "type": "method", "name": "send", "signatures": [ { "params": [ { "textRaw": "`buf` Buffer object or string. Message to be sent ", "name": "buf", "desc": "Buffer object or string. Message to be sent" }, { "textRaw": "`offset` Integer. Offset in the buffer where the message starts. ", "name": "offset", "desc": "Integer. Offset in the buffer where the message starts." }, { "textRaw": "`length` Integer. Number of bytes in the message. ", "name": "length", "desc": "Integer. Number of bytes in the message." }, { "textRaw": "`port` Integer. Destination port. ", "name": "port", "desc": "Integer. Destination port." }, { "textRaw": "`address` String. Destination hostname or IP address. ", "name": "address", "desc": "String. Destination hostname or IP address." }, { "textRaw": "`callback` Function. Called when the message has been sent. Optional. ", "name": "callback", "desc": "Function. Called when the message has been sent. Optional.", "optional": true } ] }, { "params": [ { "name": "buf" }, { "name": "offset" }, { "name": "length" }, { "name": "port" }, { "name": "address" }, { "name": "callback", "optional": true } ] } ], "desc": "

For UDP sockets, the destination port and address must be specified. A string\nmay be supplied for the address parameter, and it will be resolved with DNS.\n\n

\n

If the address is omitted or is an empty string, '0.0.0.0' or '::0' is used\ninstead. Depending on the network configuration, those defaults may or may not\nwork; it's best to be explicit about the destination address.\n\n

\n

If the socket has not been previously bound with a call to bind, it gets\nassigned a random port number and is bound to the "all interfaces" address\n('0.0.0.0' for udp4 sockets, '::0' for udp6 sockets.)\n\n

\n

An optional callback may be specified to detect DNS errors or for determining\nwhen it's safe to reuse the buf object. Note that DNS lookups delay the time\nto send for at least one tick. The only way to know for sure that the datagram\nhas been sent is by using a callback.\n\n

\n

With consideration for multi-byte characters, offset and length will\nbe calculated with respect to\nbyte length\nand not the character position.\n\n

\n

Example of sending a UDP packet to a random port on localhost;\n\n

\n
var dgram = require('dgram');\nvar message = new Buffer("Some bytes");\nvar client = dgram.createSocket("udp4");\nclient.send(message, 0, message.length, 41234, "localhost", function(err) {\n  client.close();\n});
\n

A Note about UDP datagram size\n\n

\n

The maximum size of an IPv4/v6 datagram depends on the MTU (Maximum Transmission Unit)\nand on the Payload Length field size.\n\n

\n\n

Note that it's impossible to know in advance the MTU of each link through which\na packet might travel, and that generally sending a datagram greater than\nthe (receiver) MTU won't work (the packet gets silently dropped, without\ninforming the source that the data did not reach its intended recipient).\n\n

\n" }, { "textRaw": "socket.bind(port[, address][, callback])", "type": "method", "name": "bind", "signatures": [ { "params": [ { "textRaw": "`port` Integer ", "name": "port", "desc": "Integer" }, { "textRaw": "`address` String, Optional ", "name": "address", "optional": true, "desc": "String" }, { "textRaw": "`callback` Function with no parameters, Optional. Callback when binding is done. ", "name": "callback", "desc": "Function with no parameters, Optional. Callback when binding is done.", "optional": true } ] }, { "params": [ { "name": "port" }, { "name": "address", "optional": true }, { "name": "callback", "optional": true } ] } ], "desc": "

For UDP sockets, listen for datagrams on a named port and optional\naddress. If address is not specified, the OS will try to listen on\nall addresses. After binding is done, a "listening" event is emitted\nand the callback(if specified) is called. Specifying both a\n"listening" event listener and callback is not harmful but not very\nuseful.\n\n

\n

A bound datagram socket keeps the node process running to receive\ndatagrams.\n\n

\n

If binding fails, an "error" event is generated. In rare case (e.g.\nbinding a closed socket), an Error may be thrown by this method.\n\n

\n

Example of a UDP server listening on port 41234:\n\n

\n
var dgram = require("dgram");\n\nvar server = dgram.createSocket("udp4");\n\nserver.on("error", function (err) {\n  console.log("server error:\\n" + err.stack);\n  server.close();\n});\n\nserver.on("message", function (msg, rinfo) {\n  console.log("server got: " + msg + " from " +\n    rinfo.address + ":" + rinfo.port);\n});\n\nserver.on("listening", function () {\n  var address = server.address();\n  console.log("server listening " +\n      address.address + ":" + address.port);\n});\n\nserver.bind(41234);\n// server listening 0.0.0.0:41234
\n" }, { "textRaw": "socket.bind(options[, callback])", "type": "method", "name": "bind", "signatures": [ { "params": [ { "textRaw": "`options` {Object} - Required. Supports the following properties: ", "options": [ { "textRaw": "`port` {Number} - Required. ", "name": "port", "type": "Number", "desc": "Required." }, { "textRaw": "`address` {String} - Optional. ", "name": "address", "type": "String", "desc": "Optional." }, { "textRaw": "`exclusive` {Boolean} - Optional. ", "name": "exclusive", "type": "Boolean", "desc": "Optional." } ], "name": "options", "type": "Object", "desc": "Required. Supports the following properties:" }, { "textRaw": "`callback` {Function} - Optional. ", "name": "callback", "type": "Function", "desc": "Optional.", "optional": true } ] }, { "params": [ { "name": "options" }, { "name": "callback", "optional": true } ] } ], "desc": "

The port and address properties of options, as well as the optional\ncallback function, behave as they do on a call to\nsocket.bind(port, [address], [callback])\n.\n\n

\n

If exclusive is false (default), then cluster workers will use the same\nunderlying handle, allowing connection handling duties to be shared. When\nexclusive is true, the handle is not shared, and attempted port sharing\nresults in an error. An example which listens on an exclusive port is\nshown below.\n\n

\n
socket.bind({\n  address: 'localhost',\n  port: 8000,\n  exclusive: true\n});
\n" }, { "textRaw": "socket.close()", "type": "method", "name": "close", "desc": "

Close the underlying socket and stop listening for data on it.\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "socket.address()", "type": "method", "name": "address", "desc": "

Returns an object containing the address information for a socket. For UDP sockets,\nthis object will contain address , family and port.\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "socket.setBroadcast(flag)", "type": "method", "name": "setBroadcast", "signatures": [ { "params": [ { "textRaw": "`flag` Boolean ", "name": "flag", "desc": "Boolean" } ] }, { "params": [ { "name": "flag" } ] } ], "desc": "

Sets or clears the SO_BROADCAST socket option. When this option is set, UDP packets\nmay be sent to a local interface's broadcast address.\n\n

\n" }, { "textRaw": "socket.setTTL(ttl)", "type": "method", "name": "setTTL", "signatures": [ { "params": [ { "textRaw": "`ttl` Integer ", "name": "ttl", "desc": "Integer" } ] }, { "params": [ { "name": "ttl" } ] } ], "desc": "

Sets the IP_TTL socket option. TTL stands for "Time to Live," but in this context it\nspecifies the number of IP hops that a packet is allowed to go through. Each router or\ngateway that forwards a packet decrements the TTL. If the TTL is decremented to 0 by a\nrouter, it will not be forwarded. Changing TTL values is typically done for network\nprobes or when multicasting.\n\n

\n

The argument to setTTL() is a number of hops between 1 and 255. The default on most\nsystems is 64.\n\n

\n" }, { "textRaw": "socket.setMulticastTTL(ttl)", "type": "method", "name": "setMulticastTTL", "signatures": [ { "params": [ { "textRaw": "`ttl` Integer ", "name": "ttl", "desc": "Integer" } ] }, { "params": [ { "name": "ttl" } ] } ], "desc": "

Sets the IP_MULTICAST_TTL socket option. TTL stands for "Time to Live," but in this\ncontext it specifies the number of IP hops that a packet is allowed to go through,\nspecifically for multicast traffic. Each router or gateway that forwards a packet\ndecrements the TTL. If the TTL is decremented to 0 by a router, it will not be forwarded.\n\n

\n

The argument to setMulticastTTL() is a number of hops between 0 and 255. The default on most\nsystems is 1.\n\n

\n" }, { "textRaw": "socket.setMulticastLoopback(flag)", "type": "method", "name": "setMulticastLoopback", "signatures": [ { "params": [ { "textRaw": "`flag` Boolean ", "name": "flag", "desc": "Boolean" } ] }, { "params": [ { "name": "flag" } ] } ], "desc": "

Sets or clears the IP_MULTICAST_LOOP socket option. When this option is set, multicast\npackets will also be received on the local interface.\n\n

\n" }, { "textRaw": "socket.addMembership(multicastAddress[, multicastInterface])", "type": "method", "name": "addMembership", "signatures": [ { "params": [ { "textRaw": "`multicastAddress` String ", "name": "multicastAddress", "desc": "String" }, { "textRaw": "`multicastInterface` String, Optional ", "name": "multicastInterface", "optional": true, "desc": "String" } ] }, { "params": [ { "name": "multicastAddress" }, { "name": "multicastInterface", "optional": true } ] } ], "desc": "

Tells the kernel to join a multicast group with IP_ADD_MEMBERSHIP socket option.\n\n

\n

If multicastInterface is not specified, the OS will try to add membership to all valid\ninterfaces.\n\n

\n" }, { "textRaw": "socket.dropMembership(multicastAddress[, multicastInterface])", "type": "method", "name": "dropMembership", "signatures": [ { "params": [ { "textRaw": "`multicastAddress` String ", "name": "multicastAddress", "desc": "String" }, { "textRaw": "`multicastInterface` String, Optional ", "name": "multicastInterface", "optional": true, "desc": "String" } ] }, { "params": [ { "name": "multicastAddress" }, { "name": "multicastInterface", "optional": true } ] } ], "desc": "

Opposite of addMembership - tells the kernel to leave a multicast group with\nIP_DROP_MEMBERSHIP socket option. This is automatically called by the kernel\nwhen the socket is closed or process terminates, so most apps will never need to call\nthis.\n\n

\n

If multicastInterface is not specified, the OS will try to drop membership to all valid\ninterfaces.\n\n

\n" }, { "textRaw": "socket.unref()", "type": "method", "name": "unref", "desc": "

Calling unref on a socket will allow the program to exit if this is the only\nactive socket in the event system. If the socket is already unrefd calling\nunref again will have no effect.\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "socket.ref()", "type": "method", "name": "ref", "desc": "

Opposite of unref, calling ref on a previously unrefd socket will not\nlet the program exit if it's the only socket left (the default behavior). If\nthe socket is refd calling ref again will have no effect.\n

\n", "signatures": [ { "params": [] } ] } ] } ], "type": "module", "displayName": "dgram" } ] }