查看: 2135|回复: 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 | 显示全部楼层



  1. module ReadTime
  2.   module Config
  3.     #小时,分钟,秒设置的变量编号, 默认放在了6,7,8号变量里面
  4.     HOUR   = 6
  5.     MINUTE = 7
  6.     SECOND = 8

  7.     #年月日
  8.     YEAR  = 9
  9.     MON   = 10
  10.     MDAY  = 11
  11.   end
  12. end




  13. module SAFX
  14.   module_function

  15.   def to_param(a)
  16.     case a when Integer then "L" when String then "p" end
  17.   end

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

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

  26.   def memread(addr, size)
  27.                         buf = "\0"*size
  28.                         api('Kernel32','RtlMoveMemory').call buf, addr, size
  29.                         buf
  30.         end
  31. end

  32. module ReadTime



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

  42.     def get
  43.         # get IP
  44.         baidu =  gethostbyname("open.baidu.com\0")
  45.         raise "不能ping到open.baidu.com" if baidu == 0
  46.         addr = memread(baidu+PTRLEN*3, PTRLEN)
  47.                                 addr = addr.unpack(TYPE_VOIDP).first
  48.                                 addr = memread(addr, PTRLEN)
  49.                                 addr = addr.unpack(TYPE_VOIDP).first
  50.                                 addr = memread(addr, PTRLEN)
  51.                                 u = addr.unpack("L").first

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

  58.         #make request
  59.         req = "GET /special/time/ HTTP/1.1\r\n"  <<
  60.               "host: open.baidu.com\r\n" <<
  61.               "\r\n" <<
  62.               "\r\n\r\n"
  63.         #send
  64.         send tcpclient, req, req.length, 0

  65.         #receive
  66.         buf = "\0"*1024
  67.         ret = ""
  68.         while true
  69.           len = recv tcpclient, buf, buf.length, 0
  70.           break if len < 1
  71.           ret << buf[0, len]
  72.         end

  73.         #shutdown
  74.         closesocket tcpclient

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

  77.         if number != 200
  78.           raise "HTTP错误: #{number}"
  79.         end


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

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

  92.      end
  93.   end
  94. end

  95. def 获取时间
  96.   ReadTime.get
  97. end
复制代码

点评

脚本没用吗?有用的话可以读取网络时间的啊  发表于 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.

在本版发帖返回顶部