创建UDP ping数据包以获取mumble(murmur)状态

时间:2014-05-02 09:26:40

标签: ruby sockets udp voip

我想使用ruby将udp数据包发送到mumble服务器,以获取有关当前连接用户数量的状态信息。

文档说明有使用UDP数据包的方法:http://mumble.sourceforge.net/Protocol#UDP_Ping_packet

但是我不知道如何使用ruby来制定它,因此得不到服务器的回复。

require 'socket'
sock = UDPSocket.new
sock.connect("99.99.99.99", 66666)
sock.send("00", 0)
p sock.recvfrom(1) # this does not return
sock.close

如何格式化udp数据包的数据?

1 个答案:

答案 0 :(得分:1)

这应该可以生成ping数据包:

def ping(identifier)
  v = identifier
  a = []

  while v > 256 # extract bytes from the identifier
    a << v % 256
    v = v / 256
  end
  a << v % 256  
  prefix = [0] * (8-a.length) # pad the identifier
  ([0,0,0,0] + prefix + a).pack("C*") # pack the message as bytes
end

用法:

# random 8 byte number as a message identifier - compare this to any packet 
# received to ensure you're receiving the correct response.

identifier = rand(256**8)
sock.send ping(identifier), 0

# you should get a response here if the mumble server is 
# accessible and responding to pings.
sock.recvfrom(1)