Socket
Encapsulates the datagram functionality.
New instances of dgram.Socket
are created using {@link createSocket}. The new
keyword is not to be used to create dgram.Socket
instances.
Since
v0.1.99
Properties
Functions
events.EventEmitter
Alias for emitter.on(eventName, listener)
.
Tells the kernel to join a multicast group at the given multicastAddress
and multicastInterface
using the IP_ADD_MEMBERSHIP
socket option. If the multicastInterface
argument is not specified, the operating system will choose one interface and will add membership to it. To add membership to every available interface, call addMembership
multiple times, once per interface.
Returns an object containing the address information for a socket. For UDP sockets, this object will contain address
, family
, and port
properties.
Tells the kernel to join a source-specific multicast channel at the given sourceAddress
and groupAddress
, using the multicastInterface
with the IP_ADD_SOURCE_MEMBERSHIP
socket option. If the multicastInterface
argument is not specified, the operating system will choose one interface and will add membership to it. To add membership to every available interface, call socket.addSourceSpecificMembership()
multiple times, once per interface.
For UDP sockets, causes the dgram.Socket
to listen for datagram messages on a named port
and optional address
. If port
is not specified or is 0
, the operating system will attempt to bind to a random port. If address
is not specified, the operating system will attempt to listen on all addresses. Once binding is complete, a 'listening'
event is emitted and the optional callback
function is called.
Associates the dgram.Socket
to a remote address and port. Every message sent by this handle is automatically sent to that destination. Also, the socket will only receive messages from that remote peer. Trying to call connect()
on an already connected socket will result in an ERR_SOCKET_DGRAM_IS_CONNECTED
exception. If address
is not provided, '127.0.0.1'
(for udp4
sockets) or '::1'
(for udp6
sockets) will be used by default. Once the connection is complete, a 'connect'
event is emitted and the optional callback
function is called. In case of failure, the callback
is called or, failing this, an 'error'
event is emitted.
A synchronous function that disassociates a connected dgram.Socket
from its remote address. Trying to call disconnect()
on an unbound or already disconnected socket will result in an ERR_SOCKET_DGRAM_NOT_CONNECTED
exception.
Instructs the kernel to leave a multicast group at multicastAddress
using the IP_DROP_MEMBERSHIP
socket option. This method is automatically called by the kernel when the socket is closed or the process terminates, so most apps will never have reason to call this.
Instructs the kernel to leave a source-specific multicast channel at the given sourceAddress
and groupAddress
using the IP_DROP_SOURCE_MEMBERSHIP
socket option. This method is automatically called by the kernel when the socket is closed or the process terminates, so most apps will never have reason to call this.
Synchronously calls each of the listeners registered for the event named eventName
, in the order they were registered, passing the supplied arguments to each.
Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbol
s.
Returns the current max listener value for the EventEmitter
which is either set by emitter.setMaxListeners(n)
or defaults to {@link defaultMaxListeners}.
This method throws ERR_SOCKET_BUFFER_SIZE
if called on an unbound socket.
This method throws ERR_SOCKET_BUFFER_SIZE
if called on an unbound socket.
Returns the number of listeners listening for the event named eventName
. If listener
is provided, it will return how many times the listener is found in the list of the listeners of the event.
Returns a copy of the array of listeners for the event named eventName
.
Alias for emitter.removeListener()
.
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.
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.
Adds a one-timelistener
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.
Returns a copy of the array of listeners for the event named eventName
, including any wrappers (such as those created by .once()
).
By default, binding a socket will cause it to block the Node.js process from exiting as long as the socket is open. The socket.unref()
method can be used to exclude the socket from the reference counting that keeps the Node.js process active. The socket.ref()
method adds the socket back to the reference counting and restores the default behavior.
Returns an object containing the address
, family
, and port
of the remote endpoint. This method throws an ERR_SOCKET_DGRAM_NOT_CONNECTED
exception if the socket is not connected.
Removes all listeners, or those of the specified eventName
.
Removes the specified listener
from the listener array for the event named eventName
.
Broadcasts a datagram on the socket. For connectionless sockets, the destination port
and address
must be specified. Connected sockets, on the other hand, will use their associated remote endpoint, so the port
and address
arguments must not be set.
Sets or clears the SO_BROADCAST
socket option. When set to true
, UDP packets may be sent to a local interface's broadcast address.
By default EventEmitter
s 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. 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.
All references to scope in this section are referring to IPv6 Zone Indices, which are defined by RFC * 4007. In string form, an IP with a scope index is written as 'IP%scope'
where scope is an interface name or interface number.
Sets or clears the IP_MULTICAST_LOOP
socket option. When set to true
, multicast packets will also be received on the local interface.
Sets the IP_MULTICAST_TTL
socket option. While TTL generally stands for "Time to Live", in this context it specifies the number of IP hops that a packet is allowed to travel 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.
Sets the SO_RCVBUF
socket option. Sets the maximum socket receive buffer in bytes.
Sets the SO_SNDBUF
socket option. Sets the maximum socket send buffer in bytes.
Sets the IP_TTL
socket option. While TTL generally stands for "Time to Live", in this context it specifies the number of IP hops that a packet is allowed to travel 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.
By default, binding a socket will cause it to block the Node.js process from exiting as long as the socket is open. The socket.unref()
method can be used to exclude the socket from the reference counting that keeps the Node.js process active, allowing the process to exit even if the socket is still listening.