Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Send custom UDP packets in Ruby


From: http://www.ruby-forum.com/topic/124159
Author: Bill Kelly

For yet another nifty UDP snippet see Skype-Style Firewall Busting with Ruby and UDP.


   1  
   2  
   3  require 'socket'
   4  
   5  #abort "Usage: server_addr, server_port, cmd_str" unless ARGV.length == 3
   6  
   7  UDP_RECV_TIMEOUT = 3  # seconds
   8  
   9  def q2cmd(server_addr, server_port, cmd_str)
  10    resp, sock = nil, nil
  11    begin
  12     cmd = "\377\377\377\377#{cmd_str}\0"
  13      sock = UDPSocket.open
  14      sock.send(cmd, 0, server_addr, server_port)
  15      resp = if select([sock], nil, nil, UDP_RECV_TIMEOUT)
  16        sock.recvfrom(65536)
  17      end
  18      if resp
  19        resp[0] = resp[0][4..-1]  # trim leading 0xffffffff
  20      end
  21    rescue IOError, SystemCallError
  22    ensure
  23      sock.close if sock
  24    end
  25    resp ? resp[0] : nil
  26  end
  27  
  28  # your firewall has to allow communication with IP address 67.19.248.74 (port 27912)
  29  #server, port, cmd = *ARGV
  30  server = "tastyspleen.net"
  31  port = 27912
  32  cmd = "status"
  33  
  34  result = q2cmd(server, port, cmd)
  35  puts result
  36  

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS