Created
December 21, 2010 08:59
-
-
Save masahitojp/749684 to your computer and use it in GitHub Desktop.
IPMsg.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
# -*- coding: utf-8 -*- | |
require 'socket' | |
class IPMsg | |
IPMSG_VERSION = 0x0001 | |
IPMSG_SENDMSG_CODE = 0x00000020 | |
IPMSG_PORT = 2425 | |
def initialize(user,host) | |
@user = user | |
@host = host | |
@sock = UDPSocket.new | |
#Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0) でも可 | |
end | |
def send(target, message) | |
sockaddr = Socket.sockaddr_in(IPMSG_PORT , target) | |
sendtext = makemsg(message) | |
# メッセージ送信 | |
@sock.send(sendtext, 0, sockaddr) | |
end | |
private | |
def makemsg(message) | |
# IPMSGプロトコルでの送信メッセージを作成 | |
return "1:%d:%s:%s:%d:%s" % [ | |
rand(1000), | |
@user, | |
@host, | |
IPMSG_SENDMSG_CODE, | |
message] | |
end | |
end | |
# ---------------------------------------- | |
# IPMSGでメッセージを送るユーザ名 | |
user = 'hogehoge' | |
# IPMSGでメッセージを送るPCのPC名 | |
local_host = 'fugafuga' | |
# メッセージの送り先IPアドレス | |
msg_to = 'localhost' | |
# メッセージ | |
msg = 'I love Ruby prgramming.' | |
ipmsg = IPMsg.new(user, local_host) | |
ipmsg.send(msg_to, msg) | |
# ------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment