Here is the service side.
require 'dnssd'
domains = []
enumerator = DNSSD.enumerate_domains do |reply|
domains << reply.domain
next if reply.flags.more_coming?
puts "Found domains:\n#{domains.join "\n"}"
break
end
DNSSD.register 'agent', '_agent._tcp', nil, 6464
puts "registered agent at 6464"
loop do
sleep 1
puts "agent running.."
end
And here is the client side..
require 'dnssd'
browser = DNSSD::Service.new
puts "Browsing for Agent service"
browser.browse '_agent._tcp' do |reply|
puts "Time: #{Time.new.to_f} reply: #{reply.fullname}"
puts reply
break
end
3. Asynchronous Browse and Resolve
require 'dnssd'
require 'socket'
browser = DNSSD::Service.new
puts "Browsing for Agent service"
# Make it Asynchronous ..
Thread.new do
browser.browse '_agent._tcp' do |reply|
puts "Time: #{Time.new.to_f} reply: #{reply.fullname}"
puts reply.inspect
puts reply.service_name
target = nil
resolver = DNSSD::Service.new
resolver.resolve reply do |r|
target = r.target
puts "#{r.name} on #{r.target}:#{r.port}"
# puts "\t#{r.text_record.inspect}" unless r.text_record.empty?
break unless r.flags.more_coming?
end
resolver.stop
puts "target: #{target}"
addrinfo = Socket.getaddrinfo target, nil, Socket::AF_INET
puts "Addresses for #{target}"
addrinfo.each do |addr|
puts addr.inspect
end
break
end
end
sleep 1
Enjoy!
No comments:
Post a Comment