@@ -20,66 +20,11 @@ You have to change it to this:
20
20
s.addMembership('224.0.0.114');
21
21
});
22
22
23
-
24
- ## dgram.createSocket(type[ , callback] )
25
-
26
- * ` type ` String. Either 'udp4' or 'udp6'
27
- * ` callback ` Function. Attached as a listener to ` message ` events.
28
- Optional
29
- * Returns: Socket object
30
-
31
- Creates a datagram Socket of the specified types. Valid types are ` udp4 `
32
- and ` udp6 ` .
33
-
34
- Takes an optional callback which is added as a listener for ` message ` events.
35
-
36
- Call ` socket.bind() ` if you want to receive datagrams. ` socket.bind() ` will
37
- bind to the "all interfaces" address on a random port (it does the right thing
38
- for both ` udp4 ` and ` udp6 ` sockets). You can then retrieve the address and port
39
- with ` socket.address().address ` and ` socket.address().port ` .
40
-
41
- ## dgram.createSocket(options[ , callback] )
42
- * ` options ` Object
43
- * ` callback ` Function. Attached as a listener to ` message ` events.
44
- * Returns: Socket object
45
-
46
- The ` options ` object should contain a ` type ` field of either ` udp4 ` or ` udp6 `
47
- and an optional boolean ` reuseAddr ` field.
48
-
49
- When ` reuseAddr ` is ` true ` ` socket.bind() ` will reuse the address, even if
50
- another process has already bound a socket on it. ` reuseAddr ` defaults to
51
- ` false ` .
52
-
53
- Takes an optional callback which is added as a listener for ` message ` events.
54
-
55
- Call ` socket.bind() ` if you want to receive datagrams. ` socket.bind() ` will
56
- bind to the "all interfaces" address on a random port (it does the right thing
57
- for both ` udp4 ` and ` udp6 ` sockets). You can then retrieve the address and port
58
- with ` socket.address().address ` and ` socket.address().port ` .
59
-
60
23
## Class: dgram.Socket
61
24
62
25
The dgram Socket class encapsulates the datagram functionality. It
63
26
should be created via ` dgram.createSocket(...) `
64
27
65
- ### Event: 'message'
66
-
67
- * ` msg ` Buffer object. The message
68
- * ` rinfo ` Object. Remote address information
69
-
70
- Emitted when a new datagram is available on a socket. ` msg ` is a ` Buffer ` and
71
- ` rinfo ` is an object with the sender's address information:
72
-
73
- socket.on('message', function(msg, rinfo) {
74
- console.log('Received %d bytes from %s:%d\n',
75
- msg.length, rinfo.address, rinfo.port);
76
- });
77
-
78
- ### Event: 'listening'
79
-
80
- Emitted when a socket starts listening for datagrams. This happens as soon as UDP sockets
81
- are created.
82
-
83
28
### Event: 'close'
84
29
85
30
Emitted after a socket is closed with ` close() ` . No new ` message ` events will be emitted
@@ -91,72 +36,38 @@ on this socket.
91
36
92
37
Emitted when an error occurs.
93
38
94
- ### socket.send(buf, offset, length, port, address[ , callback] )
95
-
96
- * ` buf ` Buffer object or string. Message to be sent
97
- * ` offset ` Integer. Offset in the buffer where the message starts.
98
- * ` length ` Integer. Number of bytes in the message.
99
- * ` port ` Integer. Destination port.
100
- * ` address ` String. Destination hostname or IP address.
101
- * ` callback ` Function. Called when the message has been sent. Optional.
102
-
103
- For UDP sockets, the destination port and address must be specified. A string
104
- may be supplied for the ` address ` parameter, and it will be resolved with DNS.
105
-
106
- If the address is omitted or is an empty string, ` '0.0.0.0' ` or ` '::0' ` is used
107
- instead. Depending on the network configuration, those defaults may or may not
108
- work; it's best to be explicit about the destination address.
39
+ ### Event: 'listening'
109
40
110
- If the socket has not been previously bound with a call to ` bind ` , it gets
111
- assigned a random port number and is bound to the "all interfaces" address
112
- (` '0.0.0.0' ` for ` udp4 ` sockets, ` '::0' ` for ` udp6 ` sockets.)
41
+ Emitted when a socket starts listening for datagrams. This happens as soon as UDP sockets
42
+ are created.
113
43
114
- An optional callback may be specified to detect DNS errors or for determining
115
- when it's safe to reuse the ` buf ` object. Note that DNS lookups delay the time
116
- to send for at least one tick. The only way to know for sure that the datagram
117
- has been sent is by using a callback. If an error occurs and a callback is
118
- given, the error will be the first argument to the callback. If a callback is
119
- not given, the error is emitted as an ` 'error' ` event on the ` socket ` object.
44
+ ### Event: 'message'
120
45
121
- With consideration for multi-byte characters, ` offset ` and ` length ` will
122
- be calculated with respect to
123
- [ byte length] ( buffer.html#buffer_class_method_buffer_bytelength_string_encoding )
124
- and not the character position.
46
+ * ` msg ` Buffer object. The message
47
+ * ` rinfo ` Object. Remote address information
125
48
126
- Example of sending a UDP packet to a random port on ` localhost ` ;
49
+ Emitted when a new datagram is available on a socket. ` msg ` is a ` Buffer ` and
50
+ ` rinfo ` is an object with the sender's address information:
127
51
128
- var dgram = require('dgram');
129
- var message = new Buffer("Some bytes");
130
- var client = dgram.createSocket("udp4");
131
- client.send(message, 0, message.length, 41234, "localhost", function(err) {
132
- client.close();
52
+ socket.on('message', function(msg, rinfo) {
53
+ console.log('Received %d bytes from %s:%d\n',
54
+ msg.length, rinfo.address, rinfo.port);
133
55
});
134
56
135
- ** A Note about UDP datagram size **
57
+ ### socket.addMembership(multicastAddress [ , multicastInterface ] )
136
58
137
- The maximum size of an ` IPv4/v6 ` datagram depends on the ` MTU ` ( _ Maximum Transmission Unit _ )
138
- and on the ` Payload Length ` field size.
59
+ * ` multicastAddress ` String
60
+ * ` multicastInterface ` String, Optional
139
61
140
- - The ` Payload Length ` field is ` 16 bits ` wide, which means that a normal payload
141
- cannot be larger than 64K octets including internet header and data
142
- (65,507 bytes = 65,535 − 8 bytes UDP header − 20 bytes IP header);
143
- this is generally true for loopback interfaces, but such long datagrams
144
- are impractical for most hosts and networks.
62
+ Tells the kernel to join a multicast group with ` IP_ADD_MEMBERSHIP ` socket option.
145
63
146
- - The ` MTU ` is the largest size a given link layer technology can support for datagrams.
147
- For any link, ` IPv4 ` mandates a minimum ` MTU ` of ` 68 ` octets, while the recommended ` MTU `
148
- for IPv4 is ` 576 ` (typically recommended as the ` MTU ` for dial-up type applications),
149
- whether they arrive whole or in fragments.
64
+ If ` multicastInterface ` is not specified, the OS will try to add membership to all valid
65
+ interfaces.
150
66
151
- For ` IPv6 ` , the minimum ` MTU ` is ` 1280 ` octets, however, the mandatory minimum
152
- fragment reassembly buffer size is ` 1500 ` octets.
153
- The value of ` 68 ` octets is very small, since most current link layer technologies have
154
- a minimum ` MTU ` of ` 1500 ` (like Ethernet).
67
+ ### socket.address()
155
68
156
- Note that it's impossible to know in advance the MTU of each link through which
157
- a packet might travel, and that generally sending a datagram greater than
158
- the (receiver) ` MTU ` won't work (the packet gets silently dropped, without
159
- informing the source that the data did not reach its intended recipient).
69
+ Returns an object containing the address information for a socket. For UDP sockets,
70
+ this object will contain ` address ` , ` family ` and ` port ` .
160
71
161
72
### socket.bind([ port] [ , address ] [ , callback] )
162
73
@@ -204,7 +115,6 @@ Example of a UDP server listening on port 41234:
204
115
server.bind(41234);
205
116
// server listening 0.0.0.0:41234
206
117
207
-
208
118
### socket.bind(options[ , callback] )
209
119
210
120
* ` options ` {Object} - Required. Supports the following properties:
@@ -230,16 +140,90 @@ shown below.
230
140
exclusive: true
231
141
});
232
142
233
-
234
143
### socket.close([ callback] )
235
144
236
145
Close the underlying socket and stop listening for data on it. If a callback is
237
146
provided, it is added as a listener for the [ 'close'] ( #dgram_event_close ) event.
238
147
239
- ### socket.address( )
148
+ ### socket.dropMembership(multicastAddress [ , multicastInterface ] )
240
149
241
- Returns an object containing the address information for a socket. For UDP sockets,
242
- this object will contain ` address ` , ` family ` and ` port ` .
150
+ * ` multicastAddress ` String
151
+ * ` multicastInterface ` String, Optional
152
+
153
+ Opposite of ` addMembership ` - tells the kernel to leave a multicast group with
154
+ ` IP_DROP_MEMBERSHIP ` socket option. This is automatically called by the kernel
155
+ when the socket is closed or process terminates, so most apps will never need to call
156
+ this.
157
+
158
+ If ` multicastInterface ` is not specified, the OS will try to drop membership to all valid
159
+ interfaces.
160
+
161
+ ### socket.send(buf, offset, length, port, address[ , callback] )
162
+
163
+ * ` buf ` Buffer object or string. Message to be sent
164
+ * ` offset ` Integer. Offset in the buffer where the message starts.
165
+ * ` length ` Integer. Number of bytes in the message.
166
+ * ` port ` Integer. Destination port.
167
+ * ` address ` String. Destination hostname or IP address.
168
+ * ` callback ` Function. Called when the message has been sent. Optional.
169
+
170
+ For UDP sockets, the destination port and address must be specified. A string
171
+ may be supplied for the ` address ` parameter, and it will be resolved with DNS.
172
+
173
+ If the address is omitted or is an empty string, ` '0.0.0.0' ` or ` '::0' ` is used
174
+ instead. Depending on the network configuration, those defaults may or may not
175
+ work; it's best to be explicit about the destination address.
176
+
177
+ If the socket has not been previously bound with a call to ` bind ` , it gets
178
+ assigned a random port number and is bound to the "all interfaces" address
179
+ (` '0.0.0.0' ` for ` udp4 ` sockets, ` '::0' ` for ` udp6 ` sockets.)
180
+
181
+ An optional callback may be specified to detect DNS errors or for determining
182
+ when it's safe to reuse the ` buf ` object. Note that DNS lookups delay the time
183
+ to send for at least one tick. The only way to know for sure that the datagram
184
+ has been sent is by using a callback. If an error occurs and a callback is
185
+ given, the error will be the first argument to the callback. If a callback is
186
+ not given, the error is emitted as an ` 'error' ` event on the ` socket ` object.
187
+
188
+ With consideration for multi-byte characters, ` offset ` and ` length ` will
189
+ be calculated with respect to
190
+ [ byte length] ( buffer.html#buffer_class_method_buffer_bytelength_string_encoding )
191
+ and not the character position.
192
+
193
+ Example of sending a UDP packet to a random port on ` localhost ` ;
194
+
195
+ var dgram = require('dgram');
196
+ var message = new Buffer("Some bytes");
197
+ var client = dgram.createSocket("udp4");
198
+ client.send(message, 0, message.length, 41234, "localhost", function(err) {
199
+ client.close();
200
+ });
201
+
202
+ ** A Note about UDP datagram size**
203
+
204
+ The maximum size of an ` IPv4/v6 ` datagram depends on the ` MTU ` (_ Maximum Transmission Unit_ )
205
+ and on the ` Payload Length ` field size.
206
+
207
+ - The ` Payload Length ` field is ` 16 bits ` wide, which means that a normal payload
208
+ cannot be larger than 64K octets including internet header and data
209
+ (65,507 bytes = 65,535 − 8 bytes UDP header − 20 bytes IP header);
210
+ this is generally true for loopback interfaces, but such long datagrams
211
+ are impractical for most hosts and networks.
212
+
213
+ - The ` MTU ` is the largest size a given link layer technology can support for datagrams.
214
+ For any link, ` IPv4 ` mandates a minimum ` MTU ` of ` 68 ` octets, while the recommended ` MTU `
215
+ for IPv4 is ` 576 ` (typically recommended as the ` MTU ` for dial-up type applications),
216
+ whether they arrive whole or in fragments.
217
+
218
+ For ` IPv6 ` , the minimum ` MTU ` is ` 1280 ` octets, however, the mandatory minimum
219
+ fragment reassembly buffer size is ` 1500 ` octets.
220
+ The value of ` 68 ` octets is very small, since most current link layer technologies have
221
+ a minimum ` MTU ` of ` 1500 ` (like Ethernet).
222
+
223
+ Note that it's impossible to know in advance the MTU of each link through which
224
+ a packet might travel, and that generally sending a datagram greater than
225
+ the (receiver) ` MTU ` won't work (the packet gets silently dropped, without
226
+ informing the source that the data did not reach its intended recipient).
243
227
244
228
### socket.setBroadcast(flag)
245
229
@@ -248,18 +232,12 @@ this object will contain `address` , `family` and `port`.
248
232
Sets or clears the ` SO_BROADCAST ` socket option. When this option is set, UDP packets
249
233
may be sent to a local interface's broadcast address.
250
234
251
- ### socket.setTTL(ttl)
252
-
253
- * ` ttl ` Integer
235
+ ### socket.setMulticastLoopback(flag)
254
236
255
- Sets the ` IP_TTL ` socket option. TTL stands for "Time to Live," but in this context it
256
- specifies the number of IP hops that a packet is allowed to go through. Each router or
257
- gateway that forwards a packet decrements the TTL. If the TTL is decremented to 0 by a
258
- router, it will not be forwarded. Changing TTL values is typically done for network
259
- probes or when multicasting.
237
+ * ` flag ` Boolean
260
238
261
- The argument to ` setTTL() ` is a number of hops between 1 and 255. The default on most
262
- systems is 64 .
239
+ Sets or clears the ` IP_MULTICAST_LOOP ` socket option. When this option is set, multicast
240
+ packets will also be received on the local interface .
263
241
264
242
### socket.setMulticastTTL(ttl)
265
243
@@ -273,35 +251,26 @@ decrements the TTL. If the TTL is decremented to 0 by a router, it will not be f
273
251
The argument to ` setMulticastTTL() ` is a number of hops between 0 and 255. The default on most
274
252
systems is 1.
275
253
276
- ### socket.setMulticastLoopback(flag)
277
-
278
- * ` flag ` Boolean
279
-
280
- Sets or clears the ` IP_MULTICAST_LOOP ` socket option. When this option is set, multicast
281
- packets will also be received on the local interface.
282
-
283
- ### socket.addMembership(multicastAddress[ , multicastInterface] )
284
-
285
- * ` multicastAddress ` String
286
- * ` multicastInterface ` String, Optional
254
+ ### socket.setTTL(ttl)
287
255
288
- Tells the kernel to join a multicast group with ` IP_ADD_MEMBERSHIP ` socket option.
256
+ * ` ttl ` Integer
289
257
290
- If ` multicastInterface ` is not specified, the OS will try to add membership to all valid
291
- interfaces.
258
+ Sets the ` IP_TTL ` socket option. TTL stands for "Time to Live," but in this context it
259
+ specifies the number of IP hops that a packet is allowed to go through. Each router or
260
+ gateway that forwards a packet decrements the TTL. If the TTL is decremented to 0 by a
261
+ router, it will not be forwarded. Changing TTL values is typically done for network
262
+ probes or when multicasting.
292
263
293
- ### socket.dropMembership(multicastAddress[ , multicastInterface] )
264
+ The argument to ` setTTL() ` is a number of hops between 1 and 255. The default on most
265
+ systems is 64.
294
266
295
- * ` multicastAddress ` String
296
- * ` multicastInterface ` String, Optional
267
+ ### socket.ref()
297
268
298
- Opposite of ` addMembership ` - tells the kernel to leave a multicast group with
299
- ` IP_DROP_MEMBERSHIP ` socket option. This is automatically called by the kernel
300
- when the socket is closed or process terminates, so most apps will never need to call
301
- this.
269
+ Opposite of ` unref ` , calling ` ref ` on a previously ` unref ` d socket will * not*
270
+ let the program exit if it's the only socket left (the default behavior). If
271
+ the socket is ` ref ` d calling ` ref ` again will have no effect.
302
272
303
- If ` multicastInterface ` is not specified, the OS will try to drop membership to all valid
304
- interfaces.
273
+ Returns ` socket ` .
305
274
306
275
### socket.unref()
307
276
@@ -311,10 +280,38 @@ active socket in the event system. If the socket is already `unref`d calling
311
280
312
281
Returns ` socket ` .
313
282
314
- ### socket.ref()
283
+ ## dgram.createSocket(options[ , callback] )
284
+ * ` options ` Object
285
+ * ` callback ` Function. Attached as a listener to ` message ` events.
286
+ * Returns: Socket object
315
287
316
- Opposite of ` unref ` , calling ` ref ` on a previously ` unref ` d socket will * not*
317
- let the program exit if it's the only socket left (the default behavior). If
318
- the socket is ` ref ` d calling ` ref ` again will have no effect.
288
+ The ` options ` object should contain a ` type ` field of either ` udp4 ` or ` udp6 `
289
+ and an optional boolean ` reuseAddr ` field.
319
290
320
- Returns ` socket ` .
291
+ When ` reuseAddr ` is ` true ` ` socket.bind() ` will reuse the address, even if
292
+ another process has already bound a socket on it. ` reuseAddr ` defaults to
293
+ ` false ` .
294
+
295
+ Takes an optional callback which is added as a listener for ` message ` events.
296
+
297
+ Call ` socket.bind() ` if you want to receive datagrams. ` socket.bind() ` will
298
+ bind to the "all interfaces" address on a random port (it does the right thing
299
+ for both ` udp4 ` and ` udp6 ` sockets). You can then retrieve the address and port
300
+ with ` socket.address().address ` and ` socket.address().port ` .
301
+
302
+ ## dgram.createSocket(type[ , callback] )
303
+
304
+ * ` type ` String. Either 'udp4' or 'udp6'
305
+ * ` callback ` Function. Attached as a listener to ` message ` events.
306
+ Optional
307
+ * Returns: Socket object
308
+
309
+ Creates a datagram Socket of the specified types. Valid types are ` udp4 `
310
+ and ` udp6 ` .
311
+
312
+ Takes an optional callback which is added as a listener for ` message ` events.
313
+
314
+ Call ` socket.bind() ` if you want to receive datagrams. ` socket.bind() ` will
315
+ bind to the "all interfaces" address on a random port (it does the right thing
316
+ for both ` udp4 ` and ` udp6 ` sockets). You can then retrieve the address and port
317
+ with ` socket.address().address ` and ` socket.address().port ` .
0 commit comments