A User Datagram Protocol (UDP) socket.
UDP runs on top of the Internet Protocol (IP) and was developed for applications that do not require reliability, acknowledgement, or flow control features at the transport layer. This simple protocol provides transport layer addressing in the form of UDP ports and an optional checksum capability.
UDP is a very simple protocol. Messages, so called datagrams, are sent to other hosts on an IP network without the need to set up special transmission channels or data paths beforehand. The UDP socket only needs to be opened for communication. It listens for incoming messages and sends outgoing messages on request.
This implementation supports both IPv4 and IPv6 addresses. For IPv4 addresses you must use Socket::Family::INET
family (default) or Socket::Family::INET6
for IPv6 # addresses.
Usage example:
require "socket" # Create server server = UDPSocket.new server.bind "localhost", 1234 # Create client and connect to server client = UDPSocket.new client.connect "localhost", 1234 # Send a text message to server client.send "message" # Receive text message from client message, client_addr = server.receive # Close client and server client.close server.close
The send
methods may sporadically fail with Socket::ConnectError
when sending datagrams to a non-listening server. Wrap with an exception handler to prevent raising. Example:
begin client.send(message, @destination) rescue ex : Socket::ConnectError p ex.inspect end
A host must become a member of a multicast group before it can receive datagrams sent to the group.
Drops membership to the specified group.
Returns the current value of the hoplimit
field on uni-cast packets.
The multicast hops option controls the hoplimit
field on uni-cast packets.
For hosts with multiple interfaces, each multicast transmission is sent from the primary network interface.
For hosts with multiple interfaces, each multicast transmission is sent from the primary network interface.
Sets whether transmitted multicast packets should be copied and sent back to the originator, if the host has joined the multicast group.
Reports whether transmitted multicast packets should be copied and sent back to the originator.
Receives a binary message from the previously bound address.
Receives a text message from the previously bound address.
IPSocket
Socket
Socket
Socket
IO::Evented
IO::Buffered
IO
IO
Reference
Reference
Object
Object
A host must become a member of a multicast group before it can receive datagrams sent to the group. Raises Socket::Error
if an incompatible address is provided.
Drops membership to the specified group. Memberships are automatically dropped when the socket is closed or the process exits. Raises Socket::Error
if an incompatible address is provided.
Returns the current value of the hoplimit
field on uni-cast packets. Datagrams with a hoplimit
of 1
are not forwarded beyond the local network. Multicast datagrams with a hoplimit
of 0
will not be transmitted on any network, but may be delivered locally if the sending host belongs to the destination group and multicast loopback is enabled.
The multicast hops option controls the hoplimit
field on uni-cast packets. If -1
is specified, the kernel will use a default value. If a value of 0
to 255
is specified, the packet will have the specified value as hoplimit
. Other values are considered invalid and Socket::Error
will be raised. Datagrams with a hoplimit
of 1
are not forwarded beyond the local network. Multicast datagrams with a hoplimit
of 0
will not be transmitted on any network, but may be delivered locally if the sending host belongs to the destination group and multicast loopback is enabled.
For hosts with multiple interfaces, each multicast transmission is sent from the primary network interface. This function overrides the default IPv4 interface address for subsequent transmissions. Setting the interface to 0.0.0.0
will select the default interface. Raises Socket::Error
unless the socket is IPv4 and an IPv4 address is provided.
For hosts with multiple interfaces, each multicast transmission is sent from the primary network interface. This function overrides the default IPv6 interface for subsequent transmissions. Setting the interface to index 0
will select the default interface.
Sets whether transmitted multicast packets should be copied and sent back to the originator, if the host has joined the multicast group.
Reports whether transmitted multicast packets should be copied and sent back to the originator.
Receives a binary message from the previously bound address.
require "socket" server = UDPSocket.new server.bind "localhost", 1234 message = Bytes.new(32) bytes_read, client_addr = server.receive(message)
Receives a text message from the previously bound address.
require "socket" server = UDPSocket.new server.bind("localhost", 1234) message, client_addr = server.receive
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/UDPSocket.html