class UDPSocket
UDPSocket represents a UDP/IP socket.
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/socket/0/udp_socket.rbs, line 132
def initialize: (?Integer family) -> untyped
Creates a new UDPSocket object.
address_family should be an integer, a string or a symbol: Socket::AF_INET, βAF_INETβ, :INET, etc.
require 'socket' UDPSocket.new #=> #<UDPSocket:fd 3> UDPSocket.new(Socket::AF_INET6) #=> #<UDPSocket:fd 4>
Public Instance Methods
(untyped, untyped, untyped, untyped) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/socket/0/udp_socket.rbs, line 116
def __recvfrom_nonblock: (untyped, untyped, untyped, untyped) -> untyped
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/socket/0/udp_socket.rbs, line 16
def bind: (String host, Integer port) -> void
Binds udpsocket to host:port.
u1 = UDPSocket.new u1.bind("127.0.0.1", 4913) u1.send "message-to-self", 0, "127.0.0.1", 4913 p u1.recvfrom(10) #=> ["message-to", ["AF_INET", 4913, "localhost", "127.0.0.1"]]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/socket/0/udp_socket.rbs, line 33
def connect: (String host, Integer port) -> void
Connects udpsocket to host:port.
This makes possible to send without destination address.
u1 = UDPSocket.new u1.bind("127.0.0.1", 4913) u2 = UDPSocket.new u2.connect("127.0.0.1", 4913) u2.send "uuuu", 0 p u1.recvfrom(10) #=> ["uuuu", ["AF_INET", 33230, "localhost", "127.0.0.1"]]
(Integer len, ?Integer flag, ?String outbuf, ?exception: boolish) → [ String, [ String, Integer, String, String ] ]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/socket/0/udp_socket.rbs, line 89
def recvfrom_nonblock: (Integer len, ?Integer flag, ?String outbuf, ?exception: boolish) -> [ String, [ String, Integer, String, String ] ]
Receives up to maxlen bytes from udpsocket using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. flags is zero or more of the MSG_ options. The first element of the results, mesg, is the data received. The second element, sender_inet_addr, is an array to represent the sender address.
When recvfrom(2) returns 0, Socket#recv_nonblock returns nil. In most cases it means the connection was closed, but it may also mean an empty packet was received, as the underlying API makes it impossible to distinguish these two cases.
Parameters
-
maxlen- the number of bytes to receive from the socket -
flags- zero or more of theMSG_options -
outbuf- destinationStringbuffer -
options- keyword hash, supportingexception: false
Example
require 'socket' s1 = UDPSocket.new s1.bind("127.0.0.1", 0) s2 = UDPSocket.new s2.bind("127.0.0.1", 0) s2.connect(*s1.addr.values_at(3,1)) s1.connect(*s2.addr.values_at(3,1)) s1.send "aaa", 0 begin # emulate blocking recvfrom p s2.recvfrom_nonblock(10) #=> ["aaa", ["AF_INET", 33302, "localhost.localdomain", "127.0.0.1"]] rescue IO::WaitReadable IO.select([s2]) retry end
Refer to Socket#recvfrom for the exceptions that may be thrown if the call to recvfrom_nonblock fails.
UDPSocket#recvfrom_nonblock may raise any error corresponding to recvfrom(2) failure, including Errno::EWOULDBLOCK.
If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitReadable. So IO::WaitReadable can be used to rescue the exceptions for retrying recvfrom_nonblock.
By specifying a keyword argument exception to false, you can indicate that recvfrom_nonblock should not raise an IO::WaitReadable exception, but return the symbol :wait_readable instead.
See
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/socket/0/udp_socket.rbs, line 112
def send: (String msg, ?Integer flags, ?String host, ?Integer port) -> Integer
Sends mesg via udpsocket.
flags should be a bitwise OR of Socket::MSG_* constants.
u1 = UDPSocket.new u1.bind("127.0.0.1", 4913) u2 = UDPSocket.new u2.send "hi", 0, "127.0.0.1", 4913 mesg, addr = u1.recvfrom(10) u1.send mesg, 0, addr[3], addr[1] p u2.recv(100) #=> "hi"