{ "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
Creates a datagram Socket of the specified types. Valid types are udp4
\nand udp6
.\n\n
Takes an optional callback which is added as a listener for message
events.\n\n
Call socket.bind
if you want to receive datagrams. socket.bind()
will bind\nto the "all interfaces" address on a random port (it does the right thing for\nboth udp4
and udp6
sockets). You can then retrieve the address and port\nwith socket.address().address
and socket.address().port
.\n\n
The dgram Socket class encapsulates the datagram functionality. It\nshould be created via dgram.createSocket(type, [callback])
.\n\n
Emitted when a new datagram is available on a socket. msg
is a Buffer
and rinfo
is\nan object with the sender's address information and the number of bytes in the datagram.\n\n
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
Emitted when an error occurs.\n\n
\n" } ], "methods": [ { "textRaw": "dgram.send(buf, offset, length, port, address, [callback])", "type": "method", "name": "send", "signatures": [ { "params": [ { "textRaw": "`buf` Buffer object. Message to be sent ", "name": "buf", "desc": "Buffer object. 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 IP ", "name": "address", "desc": "String. destination IP" }, { "textRaw": "`callback` Function. Callback when message is done being delivered. Optional. ", "name": "callback", "desc": "Function. Callback when message is done being delivered. 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 IP address must be specified. A string\nmay be supplied for the address
parameter, and it will be resolved with DNS. An\noptional callback may be specified to detect any DNS errors and when buf
may be\nre-used. Note that DNS lookups will delay the time that a send takes place, at\nleast until the next tick. The only way to know for sure that a send has taken place\nis to use the callback.\n\n
If the socket has not been previously bound with a call to bind
, it's\nassigned a random port number and bound to the "all interfaces" address\n(0.0.0.0 for udp4
sockets, ::0 for udp6
sockets).\n\n
Example of sending a UDP packet to a random port on localhost
;\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, bytes) {\n client.close();\n});
\nA Note about UDP datagram size\n\n
\nThe maximum size of an IPv4/v6
datagram depends on the MTU
(Maximum Transmission Unit)\nand on the Payload Length
field size.\n\n
The Payload Length
field is 16 bits
wide, which means that a normal payload\ncannot be larger than 64K octets including internet header and data\n(65,507 bytes = 65,535 − 8 bytes UDP header − 20 bytes IP header);\nthis is generally true for loopback interfaces, but such long datagrams\nare impractical for most hosts and networks.
The MTU
is the largest size a given link layer technology can support for datagrams.\nFor any link, IPv4
mandates a minimum MTU
of 68
octets, while the recommended MTU
\nfor IPv4 is 576
(typically recommended as the MTU
for dial-up type applications),\nwhether they arrive whole or in fragments.
For IPv6
, the minimum MTU
is 1280
octets, however, the mandatory minimum\nfragment reassembly buffer size is 1500
octets.\nThe value of 68
octets is very small, since most current link layer technologies have\na minimum MTU
of 1500
(like Ethernet).
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
For UDP sockets, listen for datagrams on a named port
and optional address
. If\naddress
is not specified, the OS will try to listen on all addresses.\n\n
Example of a UDP server listening on port 41234:\n\n
\nvar dgram = require("dgram");\n\nvar server = dgram.createSocket("udp4");\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": "dgram.close()",
"type": "method",
"name": "close",
"desc": "Close the underlying socket and stop listening for data on it.\n\n
\n", "signatures": [ { "params": [] } ] }, { "textRaw": "dgram.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
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
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
The argument to setTTL()
is a number of hops between 1 and 255. The default on most\nsystems is 64.\n\n
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
The argument to setMulticastTTL()
is a number of hops between 0 and 255. The default on most\nsystems is 1.\n\n
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
Tells the kernel to join a multicast group with IP_ADD_MEMBERSHIP
socket option.\n\n
If multicastInterface
is not specified, the OS will try to add membership to all valid\ninterfaces.\n\n
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
If multicastInterface
is not specified, the OS will try to drop membership to all valid\ninterfaces.\n