求助,TCP通信问题!
本帖最后由 49666684 于 2012-7-30 15:24 编辑C端发送用户名,密码的时候。服务端收不到消息。。求大大们帮忙看看。谢谢!
Server:#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
#Include <GuiTab.au3>
#include <array.au3>
#Include <Date.au3>
Global $hActivateClientSocket,$Key = "OPT"
Global $TimeOut = 1;定义超时多少分钟未验证的账号将被清除
Global $ServerIp = "127.0.0.1";你的公共IP地址
Global $ServerPort = "1099"
Global $OnlineList ;在线列表
#Region ### START Koda GUI section ### Form=
$Gui = GUICreate("Server", 633, 565, 240, 230)
$Group1 = GUICtrlCreateGroup("", 32, 16, 569, 241)
$Log = GUICtrlCreateEdit("", 40, 33, 553, 209)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("测试", 72, 280, 75, 25)
$Button4 = GUICtrlCreateButton("刷新连接", 432, 280, 75, 25)
$Group2 = GUICtrlCreateGroup("", 37, 326, 565, 225)
$ListView1 = GUICtrlCreateListView("Sock | Ip", 51, 348, 538, 192)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 120)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND")
; 开始 TCP 服务
;==============================================
TCPStartup()
$ConnectedSocket = -1
$MainSocket = TCPListen($ServerIp, $ServerPort)
If $MainSocket = -1 Then
MsgBox(64,"提示","创建监听失败,程序无法运行,请重新设置端口")
Exit
EndIf
AdlibRegister("_ReduceMemory", 1000 * 10) ;10秒整理一次内存
AdlibRegister("_AcceptAndDelList", 300) ;默认250MS检测一次是否有新客户机连接与删除旧客户机
_SetLog(_Now() & "SERVER: 网关启动成功,等待客户端连接...")
While 1
Sleep (1000)
WEnd
Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
Local $iCtrl = BitAND($wParam, 0x0000FFFF)
Local $iCode = BitShift($wParam, 16)
Switch $hWnd
Case $Gui
Switch $iCtrl
Case $Button1
Case $Button4
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND
Func WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
Local $nID = BitAND($wParam, 0x0000FFFF)
Switch $nID
Case 0xf060
If $hWnd = $Gui Then
Exit
Else
GUIDelete($hWnd)
EndIf
EndSwitch
EndFunc ;==>WM_SYSCOMMAND
Func _ReduceMemory()
$I_PID = @AutoItPID
Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $I_PID)
Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle)
DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle)
Return $ai_Return
EndFunc ;==>_ReduceMemory
Func _AcceptAndDelList();定时接受与删除列表
AdlibUnRegister("_AcceptAndDelList")
_Accept();接受新客户机
_Recv_Dellist();删除离线的客户机
AdlibRegister("_AcceptAndDelList")
EndFunc ;==>_AcceptAndDelList
Func _Accept() ;接受新客户端连接,并维护socket表
$ConnectedSocket = TCPAccept($MainSocket)
If $ConnectedSocket <> -1 Then ;<>-1表示有新客户端连接
$szIP_Accepted = SocketToIP($ConnectedSocket) ;socket转成
_SetLog(_Now() & "SERVER: 加入用户 - " & $ConnectedSocket & " Ip: " & $szIP_Accepted)
$all = UBound($OnlineList) + 1
ReDim $OnlineList[$all] ;在线列表数组增加
$OnlineList[$all - 1] = $ConnectedSocket ;socket
$OnlineList[$all - 1] = $szIP_Accepted ;ip
GUICtrlCreateListViewItem($OnlineList[$all - 1] & "|" & $OnlineList[$all - 1], $ListView1)
$post = IniRead("conn.ini", "config", "Post","")
TCPSend ($ConnectedSocket, _jiami(1, "Post"&$post, $Key, 9))
EndIf
$recv = TCPRecv($ConnectedSocket , 1024 * 50, 1)
;~ MsgBox (0,0,$recv)
If $recv <> "" Then;如果接收不为空
$recv = BinaryToString($recv, 4)
$jieguo = _jiami(0, $recv, $Key, 9)
If StringLeft($jieguo, 8) = 'Checking' Then;接受到验证成功的Post公告
$nString = StringReplace($jieguo, 'Checking', '')
$nStr = StringSplit($nString,"#")
_SetLog(_Now() & "SERVER: 用户请求验证 - " & $nStr )
EndIf
EndIf
EndFunc ;==>_Accept
Func _Recv_Dellist() ;接收客户机发来数据并删除关闭的socket列表
;~ MsgBox (0,0,UBound($OnlineList))
For $i = 1 To UBound($OnlineList) - 1
$recv = TCPSend($OnlineList[$i], "")
If @error Then
;搜索LISTVIEW中需要离线的,并删除
$a = ControlListView($Gui, "", $ListView1, "FindItem", $OnlineList[$i]) ;获取GUI上的socket的listview的行
$Del_ip = _GUICtrlListView_GetItemText($ListView1, $a, 1) ;获取要删的列的IP
_SetLog(_Now() & "SERVER: 退出用户 - " & $OnlineList[$i] & " Ip: " & $Del_ip)
_GUICtrlListView_DeleteItem($ListView1, $a);删除
;删除数组
_ArrayDelete($OnlineList, $i)
AdlibRegister("_Recv_Dellist");重新启用
ExitLoop ;这个退出用来防止删除过多的socket,如果没有这个可能导致 $recv = TCPRecv($OnlineList[$i], "") 这一句数组错误。
EndIf
Next
EndFunc ;==>_Recv_Dellist
Func _jiami($jiami_lp, $jiami_txt,$jiami_pas,$jiami_lev);数据加密
Local $tlev,$bhb,$jjxc =1,$Num
Local $jh
If $jiami_txt='' Or $jiami_pas='' Or StringLen ($jiami_pas) > 100 _
Or $jiami_lev > 9 Or Int($jiami_lev)<>$jiami_lev Or $jiami_lev < 0 Then Return -1
If $jiami_lp = 1 Then
$sosu=StringLen ($jiami_pas)
For $pa_s=1 To $sosu
$jh[$pa_s]=Asc(StringMid($jiami_pas,$pa_s,1))
$Num=$Num&$jh[$pa_s]
If $jjxc > 3 Then $jjxc=1
If $jjxc=1 Then
$bhb=Int($bhb+$jh[$pa_s])
ElseIf $jjxc=2 Then
$bhb=Int($bhb*$jh[$pa_s])
ElseIf $jjxc=3 Then
$bhb=Int($bhb-$jh[$pa_s])
EndIf
$jjxc +=1
Next
$Num=StringLeft($Num, $jiami_lev)&$bhb&StringRight($Num, $jiami_lev)
$jiami_txt=StringTrimLeft(StringToBinary($jiami_txt,2),2)
$y_si=$sosu
$j_si=1
Do
$vi=StringMid($jh[$j_si],1,1)+StringMid($jh[$y_si],StringLen ($jh[$y_si]),1)
$tempa=StringMid($jiami_txt,1,$vi-1)
$tempb=StringMid($jiami_txt,$vi)
$jiami_txt=$tempa&$jh[$j_si]&$tempb
$y_si -=1
$j_si +=1
Until $y_si <= 0 And $j_si >= $sosu
$st=StringLen ($Num)
$txtshi=StringLen ($jiami_txt)
For $kl=1 To $st
$rtemp=''
For $vn=1 To $txtshi Step StringMid($Num,$kl,1)+30
$rtemp = StringMid($jiami_txt,$vn,StringMid($Num,$kl,1)+30)&$rtemp
Next
$jiami_txt=$rtemp
Next
Return $jiami_txt
ElseIf $jiami_lp = 0 Then
$sosu=StringLen ($jiami_pas)
For $pa_s=1 To $sosu
$jh[$pa_s]=Asc(StringMid($jiami_pas,$pa_s,1))
$Num=$Num&$jh[$pa_s]
If $jjxc > 3 Then $jjxc=1
If $jjxc=1 Then
$bhb=Int($bhb+$jh[$pa_s])
ElseIf $jjxc=2 Then
$bhb=Int($bhb*$jh[$pa_s])
ElseIf $jjxc=3 Then
$bhb=Int($bhb-$jh[$pa_s])
EndIf
$jjxc +=1
Next
$Num=StringLeft($Num, $jiami_lev)&$bhb&StringRight($Num, $jiami_lev)
$st=StringLen ($Num)
Do
$txtshi=StringLen ($jiami_txt)
$rtemp=''
Do
$rtemp =$rtemp&StringRight ($jiami_txt, StringMid($Num,$st,1)+30)
$jiami_txt=StringTrimRight ($jiami_txt, StringMid($Num,$st,1)+30)
$txtshi -= StringMid($Num,$st,1)+30
Until $txtshi <= 0
$jiami_txt=$rtemp
$st -=1
Until $st <= 0
$y_si=$sosu
$j_si=1
Do
$vi=StringMid($jh[$y_si],1,1)+StringMid($jh[$j_si],StringLen ($jh[$j_si]),1)
$tempa=StringMid($jiami_txt,1,$vi-1)
$tempb=StringMid($jiami_txt,$vi+StringLen ($jh[$y_si]))
$jiami_txt=$tempa&$tempb
$y_si -=1
$j_si +=1
Until $y_si <= 0
$jiami_txt='0x'&$jiami_txt
$jiami_txt = BinaryToString($jiami_txt,2)
Return $jiami_txt
Else
Return -1
EndIf
EndFunc
Func SocketToIP($SHOCKET)
Local $sockaddr, $aRet
$sockaddr = DllStructCreate("short;ushort;uint;char")
$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
"ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
If Not @error And $aRet = 0 Then
$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
If Not @error Then $aRet = $aRet
Else
$aRet = 0
EndIf
$sockaddr = 0
Return $aRet
EndFunc ;==>SocketToIP
Func _SetLog($LogData);日志输出
Dim $xx = GUICtrlRead($Log)
If $xx = '' Then
GUICtrlSetData($Log, $LogData & @CRLF & "===========================网关系统开始工作===========================")
Else
GUICtrlSetData($Log, $LogData & @CRLF & $xx)
EndIf
EndFunc ;==>_setlog
Client:#include <IE.au3>
#include <file.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTab.au3>
#include <SQLite.au3>
#include <SQLite.dll.au3>
#include <GuiListView.au3>
#include <ImageListConstants.au3>
#include <GuiImageList.au3>
#include <GuiComboBoxEx.au3>
#include <GuiImageList.au3>
Global $Title = "客户端"
Global $Key = "OPT"
$Gui = GUICreate($Title, 360, 307)
$Group1 = GUICtrlCreateGroup("", 8, 10, 345, 289)
$Label6 = GUICtrlCreateLabel("选择服务器:", 70, 35, 80, 17)
$Combo1 =_GUICtrlComboBoxEx_Create($GUI, "", 140, 30, 150, 100)
$hImage = _GUIImageList_Create(14, 14, 5, 3)
$Group2 = GUICtrlCreateGroup("", 49, 66, 263, 37)
$revmsg = GUICtrlCreateLabel("连接服务器....", 64, 74, 236, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group3 = GUICtrlCreateGroup("", 36, 109, 289, 103)
$UserId_Input = GUICtrlCreateInput("", 141, 138, 121, 21)
GUICtrlSetFont(-1, 11, 400, 0)
GUICtrlSetLimit(-1, 15)
GUICtrlSetFont(-1, 11, 400, 0)
$UserPwd_Input = GUICtrlCreateInput("", 141, 172, 121, 21, 0x0020)
GUICtrlSetFont(-1, 11, 400, 0)
$Label1 = GUICtrlCreateLabel("用 户:", 94, 141, 43, 17)
$Label2 = GUICtrlCreateLabel("密 码:", 94, 175, 43, 17)
;~ GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group4 = GUICtrlCreateGroup("", 44, 219, 277, 61)
$Check_Btn = GUICtrlCreateButton("登陆服务器", 86, 239, 75, 25,$BS_DEFPUSHBUTTON)
$Buttonqh = GUICtrlCreateButton("切换服务器", 200, 239, 75, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW, $GUI)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND")
TCPStartUp()
Global $Socket = TCPConnect("127.0.0.1", 1099)
If $Socket = -1 Then
GUICtrlSetData($revmsg, "服务器连接失败,请联系管理员!")
_ext()
Else
GUICtrlSetData($revmsg, "服务器连接成功,请登陆!")
EndIf
While 1
Sleep(1000)
_ReduceMemory()
WEnd
Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
Local $iCtrl = BitAND($wParam, 0x0000FFFF)
Local $iCode = BitShift($wParam, 16)
Switch $hWnd
Case $Gui
Switch $iCtrl
Case $Check_Btn
$UserId = GUICtrlRead($UserId_Input)
$UserPwd = GUICtrlRead($UserPwd_Input)
$aa = md5($UserPwd)
tcpsend($Socket, _jiami(1, "Checking"&$UserId&"#"&$aa, $Key, 9))
Case $Buttonqh
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND
Func WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
Local $nID = BitAND($wParam, 0x0000FFFF)
Switch $nID
Case 0xf060
If $hWnd = $Gui Then
Exit
Else
GUIDelete($hWnd)
EndIf
EndSwitch
EndFunc ;==>WM_SYSCOMMAND
Func _ext()
TCPCloseSocket($Socket)
TCPShutdown()
Exit
EndFunc
Func _ReduceMemory()
$I_PID = @AutoItPID
Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $I_PID)
Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle)
DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle)
Return $ai_Return
EndFunc ;==>_ReduceMemory
Func MD5($sData)
Dim $PROV_RSA_FULL=1
Dim $CRYPT_VERIFYCONTEXT=0xf0000000
Dim $CALG_MD5=0x00008003
Dim $HP_HASHVAL=0x0002
$hProv = DllCall("advapi32.dll", "int", "CryptAcquireContext", "ptr*", 0, "ptr", 0, "ptr", 0, "dword", $PROV_RSA_FULL, "dword", $CRYPT_VERIFYCONTEXT)
If (@error) Or (Not $hProv) Then Return ""
$hHash = DllCall("advapi32.dll", "int", "CryptCreateHash", "ptr", $hProv, "uint", $CALG_MD5, "ptr", 0, "dword", 0, "ptr*", 0)
$hHash = $hHash
$tData = DllStructCreate("byte[" & BinaryLen($sData) & "]")
DllStructSetData($tData, 1, $sData)
$Ret = DllCall("advapi32.dll", "int", "CryptHashData", "ptr", $hHash, "ptr", DllStructGetPtr($tData), "dword", DllStructGetSize($tData), "dword", 1)
$tData = DllStructCreate("byte")
$Ret = DllCall("advapi32.dll", "int", "CryptGetHashParam", "ptr", $hHash, "dword", $HP_HASHVAL, "ptr", DllStructGetPtr($tData), "dword*", 16, "dword", 0)
If $hHash Then DllCall("advapi32.dll", "int", "CryptDestroyHash", "ptr", $hHash)
If $hProv Then DllCall("advapi32.dll", "int", "CryptReleaseContext", "ptr", $hProv, "long", 0)
Return StringTrimLeft(DllStructGetData($tData, 1),2)
EndFunc ;==>__MD5
Func _jiami($jiami_lp, $jiami_txt,$jiami_pas,$jiami_lev);数据加密
Local $tlev,$bhb,$jjxc =1,$Num
Local $jh
If $jiami_txt='' Or $jiami_pas='' Or StringLen ($jiami_pas) > 100 _
Or $jiami_lev > 9 Or Int($jiami_lev)<>$jiami_lev Or $jiami_lev < 0 Then Return -1
If $jiami_lp = 1 Then
$sosu=StringLen ($jiami_pas)
For $pa_s=1 To $sosu
$jh[$pa_s]=Asc(StringMid($jiami_pas,$pa_s,1))
$Num=$Num&$jh[$pa_s]
If $jjxc > 3 Then $jjxc=1
If $jjxc=1 Then
$bhb=Int($bhb+$jh[$pa_s])
ElseIf $jjxc=2 Then
$bhb=Int($bhb*$jh[$pa_s])
ElseIf $jjxc=3 Then
$bhb=Int($bhb-$jh[$pa_s])
EndIf
$jjxc +=1
Next
$Num=StringLeft($Num, $jiami_lev)&$bhb&StringRight($Num, $jiami_lev)
$jiami_txt=StringTrimLeft(StringToBinary($jiami_txt,2),2)
$y_si=$sosu
$j_si=1
Do
$vi=StringMid($jh[$j_si],1,1)+StringMid($jh[$y_si],StringLen ($jh[$y_si]),1)
$tempa=StringMid($jiami_txt,1,$vi-1)
$tempb=StringMid($jiami_txt,$vi)
$jiami_txt=$tempa&$jh[$j_si]&$tempb
$y_si -=1
$j_si +=1
Until $y_si <= 0 And $j_si >= $sosu
$st=StringLen ($Num)
$txtshi=StringLen ($jiami_txt)
For $kl=1 To $st
$rtemp=''
For $vn=1 To $txtshi Step StringMid($Num,$kl,1)+30
$rtemp = StringMid($jiami_txt,$vn,StringMid($Num,$kl,1)+30)&$rtemp
Next
$jiami_txt=$rtemp
Next
Return $jiami_txt
ElseIf $jiami_lp = 0 Then
$sosu=StringLen ($jiami_pas)
For $pa_s=1 To $sosu
$jh[$pa_s]=Asc(StringMid($jiami_pas,$pa_s,1))
$Num=$Num&$jh[$pa_s]
If $jjxc > 3 Then $jjxc=1
If $jjxc=1 Then
$bhb=Int($bhb+$jh[$pa_s])
ElseIf $jjxc=2 Then
$bhb=Int($bhb*$jh[$pa_s])
ElseIf $jjxc=3 Then
$bhb=Int($bhb-$jh[$pa_s])
EndIf
$jjxc +=1
Next
$Num=StringLeft($Num, $jiami_lev)&$bhb&StringRight($Num, $jiami_lev)
$st=StringLen ($Num)
Do
$txtshi=StringLen ($jiami_txt)
$rtemp=''
Do
$rtemp =$rtemp&StringRight ($jiami_txt, StringMid($Num,$st,1)+30)
$jiami_txt=StringTrimRight ($jiami_txt, StringMid($Num,$st,1)+30)
$txtshi -= StringMid($Num,$st,1)+30
Until $txtshi <= 0
$jiami_txt=$rtemp
$st -=1
Until $st <= 0
$y_si=$sosu
$j_si=1
Do
$vi=StringMid($jh[$y_si],1,1)+StringMid($jh[$j_si],StringLen ($jh[$j_si]),1)
$tempa=StringMid($jiami_txt,1,$vi-1)
$tempb=StringMid($jiami_txt,$vi+StringLen ($jh[$y_si]))
$jiami_txt=$tempa&$tempb
$y_si -=1
$j_si +=1
Until $y_si <= 0
$jiami_txt='0x'&$jiami_txt
$jiami_txt = BinaryToString($jiami_txt,2)
Return $jiami_txt
Else
Return -1
EndIf
EndFunc
怎么没有高手可以指导下么? 有防火墙么?运行服务端,,在客户端telnet看看,,
页:
[1]