ruby 簡單實現的ipmsg

從網上搜到的...瞭解一下Ruby的Socket編程

require "socket"

class IPMSG

IPMSG_PORT = 2425

VERSION = "1"

COMMAND = "32" #send msg

def initialize(user, port = IPMSG_PORT)

@user = user

@port = port

@sock = UDPSocket.new

end
def send(target, msg)

s = [VERSION,rand(100),target,@user,COMMAND,msg].join(":")

puts s
@sock.connect target, @port

@sock.send s, 0

end
end
#----------------------------------------------------------------

ipmsg = IPMSG.new("dyw")

ipmsg.send "210.25.23.174","hi,ruby ipmsg test"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章