struct UDPSocket

Overview

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/udp_socket"

# Create server
server = UDPSocket.new "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 Errno::ECONNREFUSED when sending datagrams to a non-listening server. Wrap with an exception handler to prevent raising. Example:

begin
  client.send(message, @destination)
rescue ex : Errno
  if ex.errno == Errno::ECONNREFUSED
    p ex.inspect
  end
end

Defined in:

socket/udp_socket.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from struct Struct

==(other) : Bool ==, hash(hasher) hash, inspect(io : IO) : Nil inspect, pretty_print(pp) : Nil pretty_print, to_s(io) to_s

Instance methods inherited from struct Value

==(other : JSON::Any)
==(other : YAML::Any)
==(other)
==
, dup dup

Instance methods inherited from class Object

!=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other)
===
, =~(other) =~, class class, dup dup, hash(hasher)
hash
hash
, inspect(io : IO)
inspect
inspect
, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json
to_json
, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ")
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as

Constructor methods inherited from class Object

from_json(string_or_io, root : String) : self
from_json(string_or_io) : self
from_json
, from_yaml(string_or_io : String | IO) : self from_yaml

Constructor Detail

def self.new(raw : Socket::Raw) #

Creates a UDPSocket from a raw socket.


[View source]
def self.new(family : Socket::Family = Socket::Family::INET) : UDPSocket #

Creates a UDPSocket and binds it to any available local address and port.


[View source]
def self.new(address : Socket::IPAddress, *, dns_timeout : Time::Span | Number? = nil, connect_timeout : Time::Span | Number? = nil) : UDPSocket #

Creates a UDPSocket and binds it to address.


[View source]
def self.new(host : String, port : Int32 = 0, *, dns_timeout : Time::Span | Number? = nil, connect_timeout : Time::Span | Number? = nil) : UDPSocket #

Creates a UDPSocket and binds it to address and port.

If port is 0, any available local port will be chosen.


[View source]

Class Method Detail

def self.open(address : Socket::IPAddress, *, dns_timeout : Time::Span | Number? = nil, connect_timeout : Time::Span | Number? = nil, &block) #

Creates a UDPSocket bound to address and yields it to the block.

The socket will be closed automatically when the block returns.


[View source]
def self.open(host : String, port : Int32 = 0, *, dns_timeout : Time::Span | Number? = nil, connect_timeout : Time::Span | Number? = nil, &block) #

Creates a UDPSocket bound to address and port and yields it to the block.

The socket will be closed automatically when the block returns.

If port is 0, any available local port will be chosen.


[View source]

Instance Method Detail

def bind(addr : Address | Addrinfo) : Nil #

Binds this socket to a local address.

Raises Errno if the binding fails.


[View source]
def bind(address : String, port : Int) : Nil #

Binds this socket to local address and port.

Raises Errno if the binding fails.


[View source]
def bind(port : Int) : Nil #

Binds this socket to port on any local interface.

Raises Errno if the binding fails.


[View source]
def broadcast=(val : Bool) : Bool #

[View source]
def broadcast? : Bool #

[View source]
def close : Nil #

Closes this socket.


[View source]
def closed? : Bool #

Returns true if this socket is closed.


[View source]
def connect(address : Socket::IPAddress, *, connect_timeout : Time::Span | Number? = nil) : Nil #

Connects this UDP socket to remote address.


[View source]
def connect(host : String, port : Int, *, dns_timeout : Time::Span | Number? = nil, connect_timeout : Time::Span | Number? = nil) : Nil #

Connects this UDP socket to remote address host and port.


[View source]
def local_address : Socket::IPAddress #

Returns the IPAddress for the local end of the IP socket.


[View source]
def raw : Socket::Raw #

Returns the raw socket wrapped by this UDP socket.


[View source]
def receive(message : Bytes) : Tuple(Int32, Socket::IPAddress) #

Receives a binary message from the previously bound address.

server = UDPSocket.new
server.bind "localhost", 1234

message = Bytes.new(32)
bytes_read, client_addr = server.receive(message)

[View source]
def receive(*, max_message_size = 512) : Tuple(String, Socket::IPAddress) #

Receives a text message from the previously bound address.

server = UDPSocket.new
server.bind("localhost", 1234)

message, client_addr = server.receive

[View source]
def recv_buffer_size : Int32 #

Returns the receive buffer size for this socket.


[View source]
def recv_buffer_size=(value : Int32) : Int32 #

Sets the receive buffer size for this socket.


[View source]
def remote_address : Socket::IPAddress #

Returns the IPAddress for the remote end of the IP socket.


[View source]
def remote_address? : Socket::IPAddress? #

Returns the IPAddress for the remote end of the IP socket or nil if the socket is not connected.


[View source]
def reuse_address? : Bool #

Returns true if this socket has been configured to reuse the address (see SO_REUSEADDR).


[View source]
def reuse_port? : Bool #

Returns true if this socket has been configured to reuse the port (see SO_REUSEPORT).


[View source]
def send(message) #

[View source]
def send(message, *, to addr : Socket::IPAddress) #

[View source]
def send_buffer_size : Int32 #

Returns the send buffer size for this socket.


[View source]
def send_buffer_size=(value : Int32) : Int32 #

Sets the send buffer size for this socket.


[View source]