这是个寻找CS主机的小辅助,可以在不开CS的情况下刷新主机(可以是本地局域网,也可以是指定的IP段),找到合适的主机可以直接加入

本文仅是对CS刷新及加入服务器的研究性展示,这里展示了所有源码,看完后您将对CS的刷新有一个很清晰的了解并可以非常轻松的编译出这个小辅助

1.先是几个定义

  1. Option?Base?1
  2. Public?ServerInfo(50,?8)
  3. Public?SavePointer?As?Long
  4. Public?ResponseCS?As?String
  5. Public?CSPATH?As?String
  6. Public?TimeElapsed?As?Long

2.模拟CS客户端向主机发送刷新请求:这里255.255.255.255是一个广播地址,可以刷局域网中的所有主机,如果刷互联网上的服务器可以改为其他地址,例如:izhuyue.cn,8.8.8.8,8.8.8.255等。CS主机默认端口是27015,如果同时开多个主机则依次为27016,27017。。。这里Timer3是用来计算延时的,精度50ms,没有太大的意义,仅仅是功能上的完善。

  1. cs1(1).Protocol?=?sckUDPProtocol
  2. cs1(1).RemotePort?=?27015
  3. cs1(1).RemoteHost?=?"255.255.255.255"
  4. cs1(1).SendData?Chr(255)?&?Chr(255)?&?Chr(255)?&?Chr(255)?&?"ping"?&?Chr(0)
  5. Timer3.Interval = 50
  6. Timer3.Enabled = True

3.对主机做出的回应的处理:主机可能会返回很多种数据,我们仅对刷新相关的进行处理。这里用到一个winsock控件,第4行这里是主机对刷新请求的确认,直接回复一个固定的"infostring"即可。第7行是主机回复的服务器信息,非常复杂,需要对其进行拆分。

  1. ResponseCS?=?Chr(255)?&?Chr(255)?&?Chr(255)?&?Chr(255)?&?"infostring"?&?Chr(0)
  2. Dim?strData?As?String
  3. ????cs1(Index).GetData?strData
  4. If?Len(strData)?=?6?Then
  5. ????cs1(Index).SendData?ResponseCS
  6. End?If
  7. If?Mid(strData,?5,?4)?=?"info"?Then
  8. ????RestoreData?(strData)
  9. End?If

4.紧接上一步,把主机返回的信息处理一下,包括主机名,在线人数,人数上限,地图名称,游戏版本,有无密码信息等等。我用了两个过程搞定的拆分,RestoreData和BAT,拆分后保存在数组ServerInfo()中。

  1. Public?Sub?RestoreData(strData1?As?String)
  2. ????On?Error?Resume?Next
  3. ????ServerInfo(SavePointer,?8)?=?TimeElapsed
  4. ????Dim?TempAdd?As?String
  5. ????ServerInfo(SavePointer,?1)?=?BAT(strData1,?2)
  6. ????ServerInfo(SavePointer,?2)?=?BAT(strData1,?4)
  7. ????ServerInfo(SavePointer,?3)?=?BAT(strData1,?6)
  8. ????ServerInfo(SavePointer,?4)?=?BAT(strData1,?12)
  9. ????ServerInfo(SavePointer,?5)?=?BAT(strData1,?18)
  10. ????ServerInfo(SavePointer,?6)?=?BAT(strData1,?20)
  11. ????If?ServerInfo(SavePointer,?1)?=?"46"?Then?ServerInfo(SavePointer,?1)?=?"CS1.5"
  12. ????If?ServerInfo(SavePointer,?1)?=?"47"?Then?ServerInfo(SavePointer,?1)?=?"CS1.6"
  13. ????If?Len(ServerInfo(SavePointer,?5))?>?15?Then
  14. ????????ServerInfo(SavePointer,?5)?=?Mid(ServerInfo(SavePointer,?5),?1,?15)?&?"..."
  15. ????Else
  16. ????????Dim?i?As?Long
  17. ????????Dim?k?As?Long
  18. ????????k?=?15?-?Len(ServerInfo(SavePointer,?5))
  19. ????????For?i?=?1?To?k
  20. ????????????ServerInfo(SavePointer,?5)?=?ServerInfo(SavePointer,?5)?&?"?"
  21. ????????Next
  22. ????End?If
  23. ????If?Len(ServerInfo(SavePointer,?6))?>?11?Then
  24. ????????ServerInfo(SavePointer,?6)?=?Mid(ServerInfo(SavePointer,?6),?1,?11)?&?"..."
  25. ????Else
  26. ????????Dim?m?As?Long
  27. ????????Dim?n?As?Long
  28. ????????n?=?11?-?Len(ServerInfo(SavePointer,?6))
  29. ????????For?m?=?1?To?n
  30. ????????????ServerInfo(SavePointer,?6)?=?ServerInfo(SavePointer,?6)?&?"?"
  31. ????????Next
  32. ????End?If
  33. ????TempAdd?=?TempAdd?&?ServerInfo(SavePointer,?5)?&?"?|?"?&?"PING="?&?ServerInfo(SavePointer,?8)?&?"ms"?&?"?|?"?&?ServerInfo(SavePointer,?6)?&?"?|?"?&?ServerInfo(SavePointer,?1)?&?"?|?"?&?ServerInfo(SavePointer,?3)?&?"/"?&?ServerInfo(SavePointer,?4)?&?"?|?"?&?ServerInfo(SavePointer,?2)
  34. ????ServerInfo(SavePointer,?7)?=?TempAdd
  35. ????SavePointer?=?SavePointer?+?1
  36. End?Sub
  1. Public?Function?BAT(strTemp?As?String,?strNum?As?Long)
  2. ????Dim?i?As?Long,?j?As?Long,?ResultData?As?String,?TempString?As?String
  3. ????Static?Times?As?Long
  4. ????Times?=?0
  5. ????For?i?=?18?To?Len(strTemp)
  6. ????????If?Mid(strTemp,?i,?1)?=?"\"?Then?Times?=?Times?+?1
  7. ????????If?Times?=?strNum?Then
  8. ????????????TempString?=?Mid(strTemp,?i?+?1)
  9. ????????????Exit?For
  10. ????????End?If
  11. ????Next
  12. ????For?j?=?1?To?Len(TempString)
  13. ????????If?Mid(TempString,?j,?1)?=?"\"?Then
  14. ????????????ResultData?=?Mid(TempString,?1,?j?-?1)
  15. ????????????Exit?For
  16. ????????End?If
  17. ????Next
  18. ????BAT?=?ResultData
  19. End?Function

5.对刷新结果的显示:这里需要一个Timer(计时)和List(显示),这里分两种情况,3秒内没有任何回应显示“No Servers Found”;有回应则调用上面两个模块进行处理并显示在List中。最后的那个Timer3是用来计算延时的,只是一个功能上的完善,精度是50ms,意义不大。

  1. Private?Sub?Timer1_Timer()
  2. ????Static?PointerCount?As?Long
  3. ????If?PointerCount?=?SavePointer?Then
  4. ????????If?SavePointer?=?1?Then
  5. ????????????List1.AddItem?"No?Servers?found"
  6. ????????Else
  7. ????????????Dim?k?As?Long
  8. ????????????For?k?=?1?To?SavePointer?-?1
  9. ????????????????List1.AddItem?ServerInfo(k,?7)
  10. ????????????Next
  11. ????????End?If
  12. ????????Timer1.Enabled?=?False
  13. ????Else
  14. ????????PointerCount?=?SavePointer
  15. ????End?If
  16. ????Timer3.Enabled?=?False
  17. End?Sub

6.下面是加入服务器的功能:仅仅是向CS主程序传递一个参数,仅包括IP地址即可,具体为:"cstrike.exe +connect 192.168.0.1",这里则直接将之前保存在数组ServerInfo()中的IP传递给了CS主程序。

  1. Private?Sub?CMDJOIN_Click()
  2. ????If?CSPATH?=?""?Then
  3. ????????MsgBox?"请先选中CS主程序(一般情况下是cstrike.exe)"
  4. ????????CSCommon.ShowOpen
  5. ????????CSPATH?=?CSCommon.FileName
  6. ????End?If
  7. ????Dim?CSCmd?As?String
  8. ????CSCmd?=?CSPATH?&?"?+connect?"?&?ServerInfo(List1.ListIndex?+?1,?2)
  9. ????If?CSPATH?=?""?Then
  10. ????????MsgBox?"未选中CS主程序"
  11. ????Else
  12. ????????Shell?CSCmd
  13. ????End?If
  14. End?Sub

7.计算延时的那个酱油Timer3

  1. Private?Sub?Timer3_Timer()
  2. ????TimeElapsed?=?TimeElapsed?+?50
  3. End?Sub

OK了,到这里就全部完工了,这个小辅助可以代替CS主程序进行刷新和查看服务器的功能,以上仅仅是思路及代码展示。看到这里,相信您已经有能力自行编译一个可执行文件使用了。

标签: CS1.5

评论已关闭