End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
Socket.udp_server_loop
Socket.udp_server_loop(port) {|msg, msg_src| ... }
Socket.udp_server_loop(host, port) {|msg, msg_src| ... }
ext/socket/lib/socket.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.1.3/ext/socket/lib/socket.rb, line 1026
def self.udp_server_loop(host=nil, port, &b) # :yield: message, message_source
udp_server_sockets(host, port) {|sockets|
udp_server_loop_on(sockets, &b)
}
end
creates a UDP/IP server on port and calls the block for each message arrived. The block is called with the message and its source information.
This method allocates sockets internally using port. If host is specified, it is used conjunction with port to determine the server addresses.
The msg is a string.
The msg_src is a Socket::UDPSource object. It is used for reply.
Example
# UDP/IP echo server. Socket.udp_server_loop(9261) {|msg, msg_src| msg_src.reply msg }
See also
.tcp_server_sockets
.udp_server_loop_on
.tcp_server_loop
.udp_server_recv
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0