本帖最后由 yeqing880 于 2010-11-20 14:01 编辑
运行一段时间后,报数组错误。。。。自己没看出哪里有问题,,,请大大帮帮我
还有一个问题,如果客户端连接一定时间后。会无法正常联系,但又没有掉线,无法发送信息到客户端,此情况发生在外网连接的时候!
#Region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
#include <Array.au3>
#include <mssql.au3>
#include <date.au3>
#AutoIt3Wrapper_run_debug_mode=Y
$SqlServer = ""
$SqlUID = ""
$SqlPWD = ""
$SqlDataBase = "tr"
$SqlConn = _MsSQLConnect($SqlUID, $SqlPWD, $SqlDataBase, $SqlServer)
If @error Then Exit
Opt("TrayMenuMode",3)
Opt("TrayOnEventMode",1)
$displayitem = TrayCreateItem("退出")
TrayItemSetOnEvent($displayitem,"_exit")
Local $sSrvIP = "127.0.0.1"
AdlibRegister("_EmptyScriptMem",5000)
TCPStartUp()
$MainSocket = TCPListen($sSrvIP, 9999, 5000)
If $MainSocket = -1 Then Exit
$Socket8989 = TCPListen($sSrvIP, 8989,1)
If $Socket8989 = -1 Then Exit
Global $8989,$ConnectedSocket8989
Dim $Csoket[1][2]
While 1
If $8989 <> 1 Then Accept8989()
Accept()
If $8989 <> 0 Then recv8989()
Recv()
WEnd
Func Accept8989()
$ConnectedSocket8989 = TCPAccept($Socket8989)
If $ConnectedSocket8989 >= 0 Then
TrayTip ( "GUI已连接", "GUI已连接,正常中作中",2)
$8989 = 1
EndIf
Return
EndFunc
Func recv8989()
$sRecv = TCPRecv($ConnectedSocket8989, 2048, 1);256)
If @error Then ; 客户端已关闭
$8989 = 0
TrayTip ("GUI已断开","GUI已断开",2)
Else
If $sRecv <> "" Then
$sRecv = BinaryToString($sRecv, 4)
EndIf
EndIf
EndFunc
Func _Send8899($msg)
$sMsg = StringToBinary($msg, 4)
TCPSend($ConnectedSocket8989, $sMsg)
If @error Then
return
Else
Return 1
EndIf
EndFunc
Func accept()
$Con = TCPAccept($MainSocket)
If $Con <> -1 Then
ReDim $csoket[UBound($Csoket)+1][2]
$csoket[UBound($Csoket)-1][0] =$Con
$csoket[UBound($Csoket)-1][1] = SocketToIP($Con)
$name = connread(SocketToIP($Con))
If $name = -1 Then
$sEdit = "连接事件: "&$name & "已连接"& " "&_Now()
Else
$sEdit = "连接事件: "&$name & "已连接"& " "&_Now()
_UpdateTbl($SqlConn, "date" , "online = 1","ip = '"&SocketToIP($Con)&"'")
EndIf
FileWrite ("log.txt",$sEdit)
If $8989 = 1 Then _Send8899($sEdit)
EndIf
Return
EndFunc
Func Recv()
For $s = 1 To UBound($csoket) -1
If $csoket[$s][0] <> "" Then
$sRecv = TCPRecv($csoket[$s][0], 2048, 1)
If @error Then
$name = connread($csoket[$s][1])
If $name = -1 Then
$sEdit ="断开事件: "&$csoket[$s][1] & "已断开"& " "&_Now()
Else
$sEdit ="断开事件: "&$name & "已断开"& " "&_Now()
_UpdateTbl($SqlConn, "date" , "online = 0","ip = '"&$csoket[$s][1]&"'")
_UpdateTbl($SqlConn, "date" , "lasttime = " &"'"&_Now()&"'","ip = '"&$csoket[$s][1]&"'")
EndIf
FileWrite ("log.txt",$sEdit)
If $8989 = 1 Then _Send8899($sEdit)
TCPCloseSocket($csoket[$s][0])
_ArrayDelete($csoket, $s)
ReDim $csoket[0][0] -= 1
EndIf
If $sRecv <> "" Then
$sRecv = BinaryToString($sRecv, 4)
If $sRecv = "online" Then
_send($csoket[$s][1],"hello")
$sEdit2 = "调试信息: "&$csoket[$s][1] & " > " & $sRecv
FileWrite ("log.txt",$sEdit2)
$sEdit = "发送信息: "&$csoket[$s][1] & " > " & "hello"
FileWrite ("log.txt",$sEdit)
If $8989 = 1 Then
_Send8899($sEdit2&" 回复: > hello")
EndIf
EndIf
EndIf
EndIf
Next
EndFunc
Func _send($iIP,$sendmsg)
Local $sMsg, $iIndex, $sEdit
$iIndex = _ArraySearch($csoket, $iIP)
If $iIndex = -1 Then
return 0
Else
$sMsg = StringToBinary($sendmsg, 4)
TCPSend($csoket[$iIndex][0], $sMsg)
If @error Then
return
Else
Return 1
EndIf
EndIf
EndFunc
Func connread($ip)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $SqlConn
$RS.open ("select * from date where ip='"&$ip&"'");打开数据分表
If Not $RS.eof And Not $RS.bof Then
Return $RS.Fields (0).value&$RS.Fields (2).value
Else
Return -1
EndIf
EndFunc
Func _EmptyScriptMem()
Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, _
'int', False, 'int', @AutoItPID)
Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
DllCall("kernel32.dll" , 'int', 'CloseHandle', 'int', $ai_Handle[0])
EndFunc
Func SocketToIP($Shocket)
Local $SockAddr, $aRet
$SockAddr = DllStructCreate("short;ushort;uint;char[8]")
$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $Shocket, "ptr", DllStructGetPtr($SockAddr), "int*", DllStructGetSize($SockAddr))
If NOT @error AND $aRet[0] = 0 Then
$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($SockAddr, 3))
If NOT @error Then $aRet = $aRet[0]
Else
$aRet = 0
EndIf
Return $aRet
EndFunc
Func _exit()
TCPCloseSocket($MainSocket)
TCPCloseSocket($Socket8989)
TCPShutdown()
Exit
EndFunc
|