- 注册时间
- 2007-1-3
- 最后登录
- 2019-5-19
③业余
- 积分
- 200
|
#==============================================================================
# ■ Socket
#------------------------------------------------------------------------------
# Creates and manages sockets.
#------------------------------------------------------------------------------
# Goldenaura3 -- April/10/2005
# The socket class is nearing completion. It still needs a few minor
# additions. One of the main ones is the ability to enter hostnames
# instead of strictly IP addresses.
#------------------------------------------------------------------------------
# Here's a quick sample of the socket class's usage.
#
# Example:
# # Creates a new socket object. AF_INET is our address family. You don't
# # need to worry about that right now. SOCK_STREAM is the type of
# # connection we're making. Stream based connections are kinda like opening
# # files. You're able to read and write from/to them as you please. The
# # other I'd like to mention is SOCK_DGRAM. This type is message based. It
# # is also connectionless. But, to make up for that, it is less reliable.
# socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
#
# # Attempts to connect to 123.581.321.345 on port 34.
# socket.connect("123.581.321.345", 34)
#
# # If this is a valid host then let's send something to him.
# socket.send("This is just a test!")
#
# # Let's see if we get a response. 4096 is our buffer size. In other words,
# # we won't recieve anything larger than 4096 bits.
# print socket.recv(4096)
#
# # Lastly, we'll close the socket.
# socket.close
#
# That's it! That's all that's involved in establishing connections.
#
# I'm currently working on a handbook that will go into a bit more detail, but
# this should give you some of the basics of using the socket class.
#
#==============================================================================
class Socket
#--------------------------------------------------------------------------
# ● Constants
#--------------------------------------------------------------------------
AF_UNSPEC = 0
AF_UNIX = 1
AF_INET = 2
AF_IPX = 6
AF_APPLETALK = 16
SOCK_STREAM = 1
SOCK_DGRAM = 2
SOCK_RAW = 3
SOCK_RDM = 4
SOCK_SEQPACKET = 5
#--------------------------------------------------------------------------
# ● Creates a new socket.
#--------------------------------------------------------------------------
# Example:
# socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
#--------------------------------------------------------------------------
def initialize(af, type, proto)
@sock = Win32API.new("wsock32", "socket", "lll", "l").call(af, type, proto)
@connected = false
end
#--------------------------------------------------------------------------
# ● Connects the socket to the given location.
#--------------------------------------------------------------------------
# Example:
# socket.connect("221.200.72.122", 50000)
#--------------------------------------------------------------------------
def connect(addr, port, af = AF_INET)
addr = addr.split(".")
sockaddr = [af, port, addr[0].to_i, addr[1].to_i, addr[2].to_i, addr[3].to_i, ""].pack("snC4a8")
if Win32API.new("wsock32", "connect", "ppi", "l").call(@sock, sockaddr, sockaddr.length) == 0
@connected = true
return
end
raise "无法连接服务器端!\r\n请稍后再试!"
end
#--------------------------------------------------------------------------
# ● Closes a socket.
#--------------------------------------------------------------------------
# Example:
# socket.connect("221.200.72.122", 50000) rescue(socket.close)
#--------------------------------------------------------------------------
def close
Win32API.new("wsock32", "closesocket", "p", "l").call(@sock)
end
#--------------------------------------------------------------------------
# ● Recieves incoming data.
#--------------------------------------------------------------------------
# Example:
# print socket.recv(4096)
#--------------------------------------------------------------------------
def recv(len, flags = 0)
buf = " " * len
Win32API.new("wsock32", "recv", "ppii", "l").call(@sock, buf, len, flags)
raise "与服务器连接断开!\r\n请稍后再试!" if buf.strip == ""
return buf.strip
end
#--------------------------------------------------------------------------
# ● Sends data.
#--------------------------------------------------------------------------
# Example:
# socket.send("This is a test!", 0)
#--------------------------------------------------------------------------
def send(data, flags = 0)
Win32API.new("wsock32", "send", "ppii", "l").call(@sock, data, data.length, flags)
end
#--------------------------------------------------------------------------
# ● Checks to see if the socket is connected.
#--------------------------------------------------------------------------
# Example:
# if socket.connected?
# @sprite_connected.visible = true
# end
#--------------------------------------------------------------------------
def connected?
return @connected
end
#--------------------------------------------------------------------------
# ● Checks to see if data is ready for reading.
#--------------------------------------------------------------------------
# Example:
# if socket.ready?
# print socket.recv(32)
# end
#--------------------------------------------------------------------------
def ready?
fd = [1, @sock].pack("ll")
ts = [0, 0].pack("ll")
return Win32API.new("wsock32", "select", "ipppp", "l").call(1, fd, 0, 0, ts) == 1 ? true : false
end
end |
|