5 The following shows dgram module APIs available for each platform.
7 | | Linux<br/>(Ubuntu) | Raspbian<br/>(Raspberry Pi) | Nuttx<br/>(STM32F4-Discovery) |
8 | :---: | :---: | :---: | :---: |
9 | dgram.createSocket | O | O | O |
10 | dgram.Socket.bind | O | O | O |
11 | dgram.Socket.close | O | O | O |
12 | dgram.Socket.send | O | O | O |
13 | dgram.Socket.address | O | O | X |
14 | dgram.Socket.setBroadcast | O | O | X |
15 | dgram.Socket.setTTL | O | O | X |
16 | dgram.Socket.addMembership | O | O | X |
17 | dgram.Socket.dropMembership | O | O | X |
18 | dgram.Socket.setMulticastLoopback | O | O | X |
19 | dgram.Socket.setMulticastTTL | X | X | X |
21 IoT.js provides udp connections through Dgram module.
23 You can use this module with `require('dgram')` and create sockets.
29 #### dgram.createSocket(options[, createListener])
30 #### dgram.createSocket(type[, createListener])
32 * `type: String`: it indicates an address family either `udp4` or `udp6`.
33 * `reuseAddr: Boolean`: it indicates to allow socket to bind the address and port used previously.
35 * `type: String`: it indicates an address family either 'udp4' or 'udp6'.
37 * `createListener: Function(msg, rinfo)`
41 * `family: String`: it indicates an address family either `IPv4` or `IPv6`
44 Creates a `dgram.Socket` according to `options` or `type`. (Currently we only accept `type`, `udp4`.)
46 `createListener` is automatically registered as `message` event listener.
52 ## class: dgram.Socket
54 You can create `dgram.Socket` instance with `dgram.createSocket()`.
61 * `callback: Function()`
63 Emitted when socket closed.
67 * `callback: Function()`
69 Emitted when an error occurs.
73 * `callback: Function()`
75 Emitted when socket is ready to receive data.
79 * `callback: Function(msg, rinfo)`
83 * `family: String`: it indicates an address family either `IPv4` or `IPv6`
86 Emitted when message comes to the socket.
95 Returns an object with the properties `address`, `port` and `family`. Basically the returned object is same with the object `rinfo` mentioned above.
98 #### socket.addMembership(multicastAddress[, multicastInterface])
99 * `multicastAddress: String`
100 * `multicastInterface: String`
102 Joins for socket the given multicast group with given `multicastAddress` and `multicastInterface`.
105 #### socket.dropMembership(multicastAddress[, multicastInterface])
106 * `multicastAddress: String`
107 * `multicastInterface: String`
109 Leaves for socket the given multicast group with given `multicastAddress` and `multicastInterface`.
112 #### socket.bind([port][, address][, bindListener])
113 #### socket.bind(options[, bindListener])
114 * `port: Number`, if it is not specified, OS will bind socket with a random port.
117 * `address: String`, Default: `0.0.0.0`
118 * `bindListener: Function()`
120 Binds `net.Socket` with given `address` and `port`.
122 `bindListener` is automatically registered as `listening` event listener.
125 #### socket.close([closeListener])
126 * `closeListener: Function()`
128 Stops listening data.
130 `closeListener` is registered as `close` event listener.
133 #### socket.setBroadcast(flag)
136 Sets or clears the `SO_BROADCAST` socket option.
139 #### socket.setTTL(ttl)
140 * `ttl: Number`, it should be between 1 and 255.
142 Sets the `IP_TTL` socket option.
145 #### socket.send(msg, [offset, length], port, address[, sendListener])
146 * `msg: Buffer | String | Array`
147 * `offset: Number`, is is only valid when `msg` is Buffer.
148 * `length: Number`, is is only valid when `msg` is Buffer.
150 * `address: String`, Default: `127.0.0.1` or `::1`
151 * `sendListener: Function(err, length)`
152 * `err: Null | Error`
153 * `code: String`, temporally, it is just a String of *"error"*.
154 * `errno: String`, it is same with `code`
158 * `length: Number`, it indicates the length of data.
160 Sends the message to the destination with given `address` and `port`.
162 If send operation is successfully completed, `sendListener` will be called with Null and the length of data, otherwise with Error object and the length of data.
165 #### socket.setMulticastLoopback(flag)
168 Sets or clears the `IP_MULTICAST_LOOP` socket option.
171 #### socket.setMulticastTTL(ttl)
172 * `ttl: Number`, it should be between 0 and 255.
174 Sets the `IP_MULTICAST_TTL` socket option.