查看: 2097|回复: 2

[已经解决] XP如何检测是否联网了

[复制链接]
梦石
0
星屑
50
在线时间
24 小时
注册时间
2015-12-23
回帖
13
发表于 2016-1-31 22:30:53 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
RPGXP游戏如何检测有没有联网,希望大神给个范例或者教程。
(游戏内已经有了联网脚本,只需要一个检测的就可以了。)
梦石
0
星屑
12161
在线时间
4435 小时
注册时间
2014-4-11
回帖
5616

开拓者

发表于 2016-2-1 19:58:12 | 显示全部楼层



[pre lang="ruby"]module ReadTime
  module Config
    #小时,分钟,秒设置的变量编号, 默认放在了6,7,8号变量里面
    HOUR   = 6
    MINUTE = 7
    SECOND = 8

    #年月日
    YEAR  = 9
    MON   = 10
    MDAY  = 11
  end
end




module SAFX
  module_function

  def to_param(a)
    case a when Integer then "L" when String then "p" end
  end

  def to_ptr(a)
    case a  when Integer then a when String then [a].pack('p').unpack("L").first end
  end

  def api(dll,func)
    lambda{|*args|
       Win32API.new(dll,func,args.map{|x|to_param(x)}, 'i').call(*args)
    }
  end

  def memread(addr, size)
                        buf = "\0"*size
                        api('Kernel32','RtlMoveMemory').call buf, addr, size
                        buf
        end
end

module ReadTime



  class << self
    TYPE_VOIDP = "L"
    PTRLEN = 4
    include SAFX
    extend SAFX
    %w[gethostbyname socket connect send recv closesocket].each{|name|
       define_method(name, &api('ws2_32', name))
    }
    api('ws2_32', 'WSAStartup').call 0x202, "\0"*2048

    def get
        # get IP
        baidu =  gethostbyname("open.baidu.com\0")
        raise "不能ping到open.baidu.com" if baidu == 0
        addr = memread(baidu+PTRLEN*3, PTRLEN)
                                addr = addr.unpack(TYPE_VOIDP).first
                                addr = memread(addr, PTRLEN)
                                addr = addr.unpack(TYPE_VOIDP).first
                                addr = memread(addr, PTRLEN)
                                u = addr.unpack("L").first

        #setup TCP
        tcpclient = socket(2, 1, 6)
        raise "无法初始化TCP" if tcpclient == -1
        #connect
        result = connect tcpclient, [2, 80, u].pack("SnLx8"), 16
        raise "不能连接到open.baidu.com" if result == -1

        #make request
        req = "GET /special/time/ HTTP/1.1\r\n"  <<
              "host: open.baidu.com\r\n" <<
              "\r\n" <<
              "\r\n\r\n"
        #send
        send tcpclient, req, req.length, 0

        #receive
        buf = "\0"*1024
        ret = ""
        while true
          len = recv tcpclient, buf, buf.length, 0
          break if len < 1
          ret << buf[0, len]
        end

        #shutdown
        closesocket tcpclient

        ret = ret.unpack("C*").pack("C*")
        number = ret[/HTTP\/1\.1 (\d+)/, 1].to_i

        if number != 200
          raise "HTTP错误: #{number}"
        end


        #gettime
        val = ret[/window\.baidu_time\((\d+)\)/, 1].to_i
        if val == 0
          raise "网络数据格式已变更,请联系作者"
        end
        curtime = Time.at val / 1000
        $game_variables[ReadTime::Config::HOUR] = curtime.hour
        $game_variables[ReadTime::Config::MINUTE] = curtime.min
        $game_variables[ReadTime::Config::SECOND] = curtime.sec

        $game_variables[ReadTime::Config::YEAR] = curtime.year
        $game_variables[ReadTime::Config::MDAY] = curtime.mday
        $game_variables[ReadTime::Config::MON] = curtime.mon

     end
  end
end

def 获取时间
  ReadTime.get
end[/pre]

点评

脚本没用吗?有用的话可以读取网络时间的啊  发表于 2016-2-7 00:38
骚瑞 没什么作用  发表于 2016-2-6 23:22
回复

使用道具 举报

头像被屏蔽
梦石
0
星屑
88
在线时间
631 小时
注册时间
2014-8-4
回帖
3243
发表于 2016-2-1 21:23:55 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
签名被屏蔽
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部