File: udp.rb

package info (click to toggle)
ruby-async-io 1.34.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 424 kB
  • sloc: ruby: 3,103; makefile: 4
file content (26 lines) | stat: -rw-r--r-- 536 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env ruby

require 'async'
require_relative '../lib/async/io'

endpoint = Async::IO::Endpoint.udp("localhost", 5300)

Async do |task|
	endpoint.bind do |socket|
		# This block executes for both IPv4 and IPv6 UDP sockets:
		loop do
			data, address = socket.recvfrom(1024)
			pp data
			pp address
		end
	end
	
	# This will try connecting to all addresses and yield for the first one that successfully connects:
	endpoint.connect do |socket|
		loop do
			task.sleep rand(1..10)
			socket.send "Hello World!", 0
		end
	end
end