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.crConstructors
-
.new(raw : Socket::Raw)
Creates a
UDPSocketfrom a raw socket. -
.new(family : Socket::Family = Socket::Family::INET) : UDPSocket
Creates a
UDPSocketand binds it to any available local address and port. -
.new(address : Socket::IPAddress, *, dns_timeout : Time::Span | Number? = nil, connect_timeout : Time::Span | Number? = nil) : UDPSocket
Creates a
UDPSocketand binds it to address. -
.new(host : String, port : Int32 = 0, *, dns_timeout : Time::Span | Number? = nil, connect_timeout : Time::Span | Number? = nil) : UDPSocket
Creates a
UDPSocketand binds it to address and port.
Class Method Summary
-
.open(address : Socket::IPAddress, *, dns_timeout : Time::Span | Number? = nil, connect_timeout : Time::Span | Number? = nil, &block)
Creates a
UDPSocketbound to address and yields it to the block. -
.open(host : String, port : Int32 = 0, *, dns_timeout : Time::Span | Number? = nil, connect_timeout : Time::Span | Number? = nil, &block)
Creates a
UDPSocketbound to address and port and yields it to the block.
Instance Method Summary
-
#bind(addr : Address | Addrinfo) : Nil
Binds this socket to a local address.
-
#bind(address : String, port : Int) : Nil
Binds this socket to local address and port.
-
#bind(port : Int) : Nil
Binds this socket to port on any local interface.
- #broadcast=(val : Bool) : Bool
- #broadcast? : Bool
-
#close : Nil
Closes this socket.
-
#closed? : Bool
Returns
trueif this socket is closed. -
#connect(address : Socket::IPAddress, *, connect_timeout : Time::Span | Number? = nil) : Nil
Connects this UDP socket to remote address.
-
#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.
-
#local_address : Socket::IPAddress
Returns the
IPAddressfor the local end of the IP socket. -
#raw : Socket::Raw
Returns the raw socket wrapped by this UDP socket.
-
#receive(message : Bytes) : Tuple(Int32, Socket::IPAddress)
Receives a binary message from the previously bound address.
-
#receive(*, max_message_size = 512) : Tuple(String, Socket::IPAddress)
Receives a text message from the previously bound address.
-
#recv_buffer_size : Int32
Returns the receive buffer size for this socket.
-
#recv_buffer_size=(value : Int32) : Int32
Sets the receive buffer size for this socket.
-
#remote_address : Socket::IPAddress
Returns the
IPAddressfor the remote end of the IP socket. -
#remote_address? : Socket::IPAddress?
Returns the
IPAddressfor the remote end of the IP socket ornilif the socket is not connected. -
#reuse_address? : Bool
Returns
trueif this socket has been configured to reuse the address (seeSO_REUSEADDR). -
#reuse_port? : Bool
Returns
trueif this socket has been configured to reuse the port (seeSO_REUSEPORT). - #send(message)
- #send(message, *, to addr : Socket::IPAddress)
-
#send_buffer_size : Int32
Returns the send buffer size for this socket.
-
#send_buffer_size=(value : Int32) : Int32
Sets the send buffer size for this socket.
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) : selffrom_json(string_or_io) : self from_json, from_yaml(string_or_io : String | IO) : self from_yaml
Constructor Detail
Creates a UDPSocket and binds it to any available local address and port.
Creates a UDPSocket and binds it to address.
Creates a UDPSocket and binds it to address and port.
If port is 0, any available local port will be chosen.
Class Method Detail
Creates a UDPSocket bound to address and yields it to the block.
The socket will be closed automatically when the block returns.
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.
Instance Method Detail
Binds this socket to a local address.
Raises Errno if the binding fails.
Binds this socket to local address and port.
Raises Errno if the binding fails.
Binds this socket to port on any local interface.
Raises Errno if the binding fails.
Connects this UDP socket to remote address.
Connects this UDP socket to remote address host and port.
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)Receives a text message from the previously bound address.
server = UDPSocket.new
server.bind("localhost", 1234)
message, client_addr = server.receiveSets the receive buffer size for this socket.
Returns the IPAddress for the remote end of the IP socket or nil if the
socket is not connected.
Returns true if this socket has been configured to reuse the address (see SO_REUSEADDR).
Returns true if this socket has been configured to reuse the port (see SO_REUSEPORT).
Sets the send buffer size for this socket.