幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1163|回复: 2

[RMXP] 解释下这个脚本的意思

[复制链接]

9

主题

18

帖子

200

积分

③业余

积分
200
发表于 2008-1-16 19:23:42 | 显示全部楼层 |阅读模式
#==============================================================================
# ■  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
回复

使用道具 举报

9

主题

18

帖子

200

积分

③业余

积分
200
 楼主| 发表于 2008-1-16 19:24:36 | 显示全部楼层
我感觉 改上边那些脚本IP端口信息 不管用呢?
回复 支持 反对

使用道具 举报

32

主题

1176

帖子

10216万

积分

⑥精研

大家都爱好少年

积分
102162186
发表于 2008-1-16 21:53:54 | 显示全部楼层
[s:5] 你不会直接改了注解里的吧………………
战,然后死!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2025-6-21 07:25 , Processed in 0.011239 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表