寻找CS服务器——简单的CS辅助程序
这是个寻找CS主机的小辅助,可以在不开CS的情况下刷新主机(可以是本地局域网,也可以是指定的IP段),找到合适的主机可以直接加入
本文仅是对CS刷新及加入服务器的研究性展示,这里展示了所有源码,看完后您将对CS的刷新有一个很清晰的了解并可以非常轻松的编译出这个小辅助
1.先是几个定义
- Option?Base?1
- Public?ServerInfo(50,?8)
- Public?SavePointer?As?Long
- Public?ResponseCS?As?String
- Public?CSPATH?As?String
- 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,没有太大的意义,仅仅是功能上的完善。
- cs1(1).Protocol?=?sckUDPProtocol
- cs1(1).RemotePort?=?27015
- cs1(1).RemoteHost?=?"255.255.255.255"
- cs1(1).SendData?Chr(255)?&?Chr(255)?&?Chr(255)?&?Chr(255)?&?"ping"?&?Chr(0)
- Timer3.Interval = 50
- Timer3.Enabled = True
3.对主机做出的回应的处理:主机可能会返回很多种数据,我们仅对刷新相关的进行处理。这里用到一个winsock控件,第4行这里是主机对刷新请求的确认,直接回复一个固定的"infostring"即可。第7行是主机回复的服务器信息,非常复杂,需要对其进行拆分。
- ResponseCS?=?Chr(255)?&?Chr(255)?&?Chr(255)?&?Chr(255)?&?"infostring"?&?Chr(0)
- Dim?strData?As?String
- ????cs1(Index).GetData?strData
- If?Len(strData)?=?6?Then
- ????cs1(Index).SendData?ResponseCS
- End?If
- If?Mid(strData,?5,?4)?=?"info"?Then
- ????RestoreData?(strData)
- End?If
4.紧接上一步,把主机返回的信息处理一下,包括主机名,在线人数,人数上限,地图名称,游戏版本,有无密码信息等等。我用了两个过程搞定的拆分,RestoreData和BAT,拆分后保存在数组ServerInfo()中。
- Public?Sub?RestoreData(strData1?As?String)
- ????On?Error?Resume?Next
- ????ServerInfo(SavePointer,?8)?=?TimeElapsed
- ????Dim?TempAdd?As?String
- ????ServerInfo(SavePointer,?1)?=?BAT(strData1,?2)
- ????ServerInfo(SavePointer,?2)?=?BAT(strData1,?4)
- ????ServerInfo(SavePointer,?3)?=?BAT(strData1,?6)
- ????ServerInfo(SavePointer,?4)?=?BAT(strData1,?12)
- ????ServerInfo(SavePointer,?5)?=?BAT(strData1,?18)
- ????ServerInfo(SavePointer,?6)?=?BAT(strData1,?20)
- ????If?ServerInfo(SavePointer,?1)?=?"46"?Then?ServerInfo(SavePointer,?1)?=?"CS1.5"
- ????If?ServerInfo(SavePointer,?1)?=?"47"?Then?ServerInfo(SavePointer,?1)?=?"CS1.6"
- ????If?Len(ServerInfo(SavePointer,?5))?>?15?Then
- ????????ServerInfo(SavePointer,?5)?=?Mid(ServerInfo(SavePointer,?5),?1,?15)?&?"..."
- ????Else
- ????????Dim?i?As?Long
- ????????Dim?k?As?Long
- ????????k?=?15?-?Len(ServerInfo(SavePointer,?5))
- ????????For?i?=?1?To?k
- ????????????ServerInfo(SavePointer,?5)?=?ServerInfo(SavePointer,?5)?&?"?"
- ????????Next
- ????End?If
- ????If?Len(ServerInfo(SavePointer,?6))?>?11?Then
- ????????ServerInfo(SavePointer,?6)?=?Mid(ServerInfo(SavePointer,?6),?1,?11)?&?"..."
- ????Else
- ????????Dim?m?As?Long
- ????????Dim?n?As?Long
- ????????n?=?11?-?Len(ServerInfo(SavePointer,?6))
- ????????For?m?=?1?To?n
- ????????????ServerInfo(SavePointer,?6)?=?ServerInfo(SavePointer,?6)?&?"?"
- ????????Next
- ????End?If
- ????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)
- ????ServerInfo(SavePointer,?7)?=?TempAdd
- ????SavePointer?=?SavePointer?+?1
- End?Sub
- Public?Function?BAT(strTemp?As?String,?strNum?As?Long)
- ????Dim?i?As?Long,?j?As?Long,?ResultData?As?String,?TempString?As?String
- ????Static?Times?As?Long
- ????Times?=?0
- ????For?i?=?18?To?Len(strTemp)
- ????????If?Mid(strTemp,?i,?1)?=?"\"?Then?Times?=?Times?+?1
- ????????If?Times?=?strNum?Then
- ????????????TempString?=?Mid(strTemp,?i?+?1)
- ????????????Exit?For
- ????????End?If
- ????Next
- ????For?j?=?1?To?Len(TempString)
- ????????If?Mid(TempString,?j,?1)?=?"\"?Then
- ????????????ResultData?=?Mid(TempString,?1,?j?-?1)
- ????????????Exit?For
- ????????End?If
- ????Next
- ????BAT?=?ResultData
- End?Function
5.对刷新结果的显示:这里需要一个Timer(计时)和List(显示),这里分两种情况,3秒内没有任何回应显示“No Servers Found”;有回应则调用上面两个模块进行处理并显示在List中。最后的那个Timer3是用来计算延时的,只是一个功能上的完善,精度是50ms,意义不大。
- Private?Sub?Timer1_Timer()
- ????Static?PointerCount?As?Long
- ????If?PointerCount?=?SavePointer?Then
- ????????If?SavePointer?=?1?Then
- ????????????List1.AddItem?"No?Servers?found"
- ????????Else
- ????????????Dim?k?As?Long
- ????????????For?k?=?1?To?SavePointer?-?1
- ????????????????List1.AddItem?ServerInfo(k,?7)
- ????????????Next
- ????????End?If
- ????????Timer1.Enabled?=?False
- ????Else
- ????????PointerCount?=?SavePointer
- ????End?If
- ????Timer3.Enabled?=?False
- End?Sub
6.下面是加入服务器的功能:仅仅是向CS主程序传递一个参数,仅包括IP地址即可,具体为:"cstrike.exe +connect 192.168.0.1",这里则直接将之前保存在数组ServerInfo()中的IP传递给了CS主程序。
- Private?Sub?CMDJOIN_Click()
- ????If?CSPATH?=?""?Then
- ????????MsgBox?"请先选中CS主程序(一般情况下是cstrike.exe)"
- ????????CSCommon.ShowOpen
- ????????CSPATH?=?CSCommon.FileName
- ????End?If
- ????Dim?CSCmd?As?String
- ????CSCmd?=?CSPATH?&?"?+connect?"?&?ServerInfo(List1.ListIndex?+?1,?2)
- ????If?CSPATH?=?""?Then
- ????????MsgBox?"未选中CS主程序"
- ????Else
- ????????Shell?CSCmd
- ????End?If
- End?Sub
7.计算延时的那个酱油Timer3
- Private?Sub?Timer3_Timer()
- ????TimeElapsed?=?TimeElapsed?+?50
- End?Sub
OK了,到这里就全部完工了,这个小辅助可以代替CS主程序进行刷新和查看服务器的功能,以上仅仅是思路及代码展示。看到这里,相信您已经有能力自行编译一个可执行文件使用了。
评论已关闭