Encapsulates the datagram functionality. It should be created via Dgram.createSocket.

Methods

addMembership(multicastAddress:String, ?multicastInterface:String):Void

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

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

address():SocketAdress

Returns an object containing the address information for a socket.

bind(?callback:() ‑> Void):Void

bind(options:SocketBindOptions, ?callback:() ‑> Void):Void

bind(port:Int, address:String, ?callback:() ‑> Void):Void

bind(port:Int, ?callback:() ‑> Void):Void

Listen for datagrams on a named port and optional address. If port is not specified, the OS will try to bind to a random port. If address is not specified, the OS will try to listen on all addresses. After binding is done, a "listening" event is emitted and the callback (if specified) is called. Specifying both a "listening" event listener and callback is not harmful but not very useful.

A bound datagram socket keeps the node process running to receive datagrams.

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

close(?callback:() ‑> Void):Void

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

If a callback is provided, it is added as a listener for the 'close' event.

dropMembership(multicastAddress:String, ?multicastInterface:String):Void

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

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

ref():Void

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

send(buf:Buffer, offset:Int, length:Int, port:Int, address:String, ?callback:(Error, Int) ‑> Void):Void

The destination port and address must be specified. A string may be supplied for the address parameter, and it will be resolved with DNS.

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

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

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

setBroadcast(flag:Bool):Void

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

setMulticastLoopback(flag:Bool):Void

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

setMulticastTTL(ttl:Int):Void

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

The argument to setMulticastTTL is a number of hops between 0 and 255. The default on most systems is 1.

setTTL(ttl:Int):Void

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

The argument to setTTL is a number of hops between 1 and 255. The default on most systems is 64.

unref():Void

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

Inherited Variables

Inherited Methods

Defined by EventEmitter

addListener<T>(eventName:Event<T>, listener:T):TSelf

emit<T>(eventName:Event<T>, args:Rest<Dynamic>):Bool

Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

See also:

eventNames():Array<EitherType<String, Symbol>>

Returns an array listing the events for which the emitter has registered listeners. The values in the array will be strings or Symbols.

See also:

getMaxListeners():Int

Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to EventEmitter.defaultMaxListeners.

See also:

listenerCount<T>(eventName:Event<T>):Int

Returns the number of listeners listening to the event named eventName.

See also:

listeners<T>(eventName:Event<T>):Array<T>

Returns a copy of the array of listeners for the event named eventName.

See also:

off<T>(eventName:Event<T>, listener:T):TSelf

on<T>(eventName:Event<T>, listener:T):TSelf

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

See also:

once<T>(eventName:Event<T>, listener:T):TSelf

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

See also:

prependListener<T>(eventName:Event<T>, listener:T):TSelf

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

See also:

prependOnceListener<T>(eventName:Event<T>, listener:T):TSelf

Adds a one-time listener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

See also:

rawListeners<T>(eventName:Event<T>):Array<T>

Returns a copy of the array of listeners for the event named eventName, including any wrappers (such as those created by .once()).

See also:

removeAllListeners<T>(?eventName:Event<T>):TSelf

Removes all listeners, or those of the specified eventName.

See also:

removeListener<T>(eventName:Event<T>, listener:T):TSelf

Removes the specified listener from the listener array for the event named eventName.

See also:

setMaxListeners(n:Int):Void

By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps finding memory leaks. Obviously, not all events should be limited to just 10 listeners. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) to indicate an unlimited number of listeners.

See also: