回复 3# haijie1223
1.启动服务端
2.注册成功
3.账户名相同才能注册失败
服务端代码
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <file.au3>
#NoTrayIcon
#RequireAdmin
Global $ipAddress="", $Port=0 ,$isStart=False , $iListenSocket=0 , $fengefu="-|-|-"
Global $REGISTESUCESS="sucess" , $REGISTEFAIL="error"
Opt("GUIOnEventMode", 1)
#Region 服务端界面
$formRegisteServer = GUICreate("服务端 v 1.0 Code by 疯子QQ14958306", 612, 408, 521, 96)
GUISetOnEvent($GUI_EVENT_CLOSE, "formRegisteServerClose")
$group_server = GUICtrlCreateGroup("服务器设置", 0, 0, 609, 49, BitOR($GUI_SS_DEFAULT_GROUP,$BS_FLAT))
$lab_ip = GUICtrlCreateLabel("IP地址", 14, 24, 44, 17)
GUICtrlSetFont(-1, 10, 400, 0, "宋体")
$combIP = GUICtrlCreateCombo("", 68, 21, 184, 25, BitOR($CBS_DROPDOWNLIST,$CBS_AUTOHSCROLL))
GUICtrlSetFont(-1, 10, 400, 0, "宋体")
$btnChongzai = GUICtrlCreateButton("重载", 256, 18, 35, 25)
GUICtrlSetFont(-1, 10, 400, 0, "宋体")
GUICtrlSetOnEvent(-1, "btnChongzaiClick")
$labPort = GUICtrlCreateLabel("端口地址", 296, 24, 56, 17)
GUICtrlSetFont(-1, 10, 400, 0, "宋体")
$inputPort = GUICtrlCreateInput("65432", 365, 21, 54, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 10, 400, 0, "宋体")
$btnStartServer = GUICtrlCreateButton("开始服务", 424, 18, 81, 25)
GUICtrlSetFont(-1, 10, 400, 0, "宋体")
GUICtrlSetOnEvent(-1, "btnStartServerClick")
$btnStopServer = GUICtrlCreateButton("停止服务", 512, 18, 81, 25)
GUICtrlSetFont(-1, 10, 400, 0, "宋体")
GUICtrlSetOnEvent(-1, "btnStopServerClick")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$group_Log = GUICtrlCreateGroup("分发日志", 0, 50, 609, 353, BitOR($GUI_SS_DEFAULT_GROUP,$BS_FLAT), $WS_EX_TRANSPARENT)
$listviewLog = GUICtrlCreateListView("IP地址|接收内容|处理结果", 0, 72, 605, 326, BitOR($LVS_REPORT,$LVS_SINGLESEL,$LVS_ALIGNLEFT), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 180)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 280)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 80)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion
btnChongzaiClick()
GUICtrlSetState($btnStopServer,$GUI_DISABLE)
TCPStartup()
GUISetState(@SW_SHOW)
While 1
Sleep(10)
If $isStart=True Then
Local $iSocket = 0
$iSocket = TCPAccept($iListenSocket)
If @error Then
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "无法接收传入连接!")
ContinueLoop
ElseIf $iSocket <> -1 Then
; 检索与接受套接字关联的 IP 地址, 并将其分配到一个局部变量.
Local $sClientIPAddress = SocketToIP($iSocket)
Local $sReceived = TCPRecv($iSocket, 1024)
Local $sReceivedArray=StringSplit($sReceived,$fengefu , 1)
If $sReceivedArray[1]="REGISTE" Then
Local $sReceivedText=BinaryToString($sReceivedArray[2])&"-"&BinaryToString($sReceivedArray[3])&"-"&BinaryToString($sReceivedArray[4])
If CheckIsHave($sReceivedText)=False Then
TCPSend($iSocket, $REGISTESUCESS)
ShowLog($sClientIPAddress & "|"&$sReceivedText&"|"& $REGISTESUCESS) ; 显示成功消息与客户端的 IP 地址.
Else
TCPSend($iSocket, $REGISTEFAIL)
ShowLog($sClientIPAddress & "|"&$sReceivedText&"|"& $REGISTEFAIL) ; 显示成功消息与客户端的 IP 地址.
EndIf
EndIf
TCPCloseSocket($iSocket)
EndIf
EndIf
WEnd
Func btnChongzaiClick()
Local $combText=""
If @IPAddress1<>"" And @IPAddress1<>"0.0.0.0" Then $combText = $combText&@IPAddress1&"|"
If @IPAddress2<>"" And @IPAddress2<>"0.0.0.0" Then $combText = $combText&@IPAddress2&"|"
If @IPAddress3<>"" And @IPAddress3<>"0.0.0.0" Then $combText = $combText&@IPAddress3&"|"
If @IPAddress4<>"" And @IPAddress4<>"0.0.0.0" Then $combText = $combText&@IPAddress4
GUICtrlSetData($combIP,"")
GUICtrlSetData($combIP,$combText,@IPAddress1)
EndFunc
Func btnStartServerClick()
If inputPortChange()=False Then Return MsgBox(48+262144+8192,"警告","端口有误!",5,WinGetHandle($formRegisteServer))
$Port = GUICtrlRead($inputPort)
$ipAddress=GUICtrlRead($combIP)
If $ipAddress="" Then Return MsgBox(48+262144+8192,"警告","IP有误!",5,WinGetHandle($formRegisteServer))
$iListenSocket = TCPListen($ipAddress, $Port, 1000)
If @error Then Return MsgBox(16, "错误!", "不能监听! ")
$isStart=True
ShowLog("服务已启动,等待客户端连接...")
GUICtrlSetState($btnStartServer,$GUI_DISABLE)
GUICtrlSetState($btnStopServer,$GUI_ENABLE)
EndFunc
Func btnStopServerClick()
If 6=MsgBox(32+4+262144+8192 ,"提示","确定要停止服务吗?",5,WinGetHandle($formRegisteServer)) Then
TCPCloseSocket($iListenSocket)
$isStart=False
GUICtrlSetState($btnStartServer,$GUI_ENABLE)
EndIf
EndFunc
Func formRegisteServerClose()
TCPShutdown() ; 关闭 TCP 服务.
Exit
EndFunc
Func inputPortChange()
Local $porttext=Int(GUICtrlRead($inputPort))
If $porttext>0 And $porttext<65535 Then
Return True
Else
Return False
EndIf
EndFunc
Func CheckIsHave($_str)
Local $_itemCount=_GUICtrlListView_GetItemCount($listviewLog)
For $_i=0 To $_itemCount-1
Local $_strToCheck=_GUICtrlListView_GetItemText($listviewLog,$_i,1)
If StringCompare($_str,$_strToCheck)=0 Then Return True
Next
Return False
EndFunc
Func SocketToIP($iSocket)
Local $tSockAddr = 0, $aRet = 0
$tSockAddr = DllStructCreate("short;ushort;uint;char[8]")
$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $iSocket, "ptr", DllStructGetPtr($tSockAddr), "int*", DllStructGetSize($tSockAddr))
If Not @error And $aRet[0] = 0 Then
$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($tSockAddr, 3))
If Not @error Then Return $aRet[0]
EndIf
Return 0
EndFunc ;==>SocketToIP
Func ShowLog($_str)
If $_str<>"" Then GUICtrlCreateListViewItem($_str , $listviewLog)
EndFunc
客户端代码
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
Global $cfgFile=@ScriptDir&"\ServerCfg.ini" , $serverIpCfgName="IP地址", $serverPortCfgName="端口" , $fengefu="-|-|-"
Global $REGISTESUCESS="sucess" , $REGISTEFAIL="error"
Global $sIPAddress=IniRead($cfgFile,"Set",$serverIpCfgName,"0.0.0.0")
Global $iPort=IniRead($cfgFile,"Set",$serverPortCfgName,"65432")
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=C:\Users\XYF\Desktop\formTaskClient.kxf
$formRegiste = GUICreate("注册账号", 332, 220, 265, 270)
GUISetOnEvent($GUI_EVENT_CLOSE, "formRegisteClose")
$group_server = GUICtrlCreateGroup("目标服务器设置", 0, 0, 329, 49, BitOR($GUI_SS_DEFAULT_GROUP,$BS_FLAT))
$labIP = GUICtrlCreateLabel("IP地址", 14, 24, 44, 17)
GUICtrlSetFont(-1, 10, 400, 0, "宋体")
$labPort = GUICtrlCreateLabel("端口地址", 208, 24, 56, 17)
GUICtrlSetFont(-1, 10, 400, 0, "宋体")
$inputPort = GUICtrlCreateInput($iPort, 269, 21, 54, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 10, 400, 0, "宋体")
$inputIPAddress = _GUICtrlIpAddress_Create($formRegiste, 64, 21, 130, 21)
_GUICtrlIpAddress_Set($inputIPAddress, $sIPAddress)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$group_account = GUICtrlCreateGroup("注册账号", 0, 56, 329, 161)
$Label1 = GUICtrlCreateLabel("账号", 15, 80, 52, 17, $SS_RIGHT)
$inputAccount = GUICtrlCreateInput("22222", 75, 77, 247, 21)
$inputPwd1 = GUICtrlCreateInput("123123", 75, 109, 247, 21)
$Label2 = GUICtrlCreateLabel("密码", 15, 112, 52, 17, $SS_RIGHT)
$inputPwd2 = GUICtrlCreateInput("123123", 75, 143, 247, 21)
$Label3 = GUICtrlCreateLabel("确认密码", 15, 146, 52, 17, $SS_RIGHT)
$btnRegiste = GUICtrlCreateButton("注册账号", 104, 176, 105, 33)
GUICtrlSetOnEvent(-1, "btnRegisteClick")
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
Sleep(100)
WEnd
Func btnRegisteClick()
; 分配局部变量回送 IP 地址和端口.
Local $tempArray = _GUICtrlIpAddress_GetArray($inputIPAddress)
If CheckIpAddress($tempArray)=False Then Return MsgBox(48+262144+8192,"警告","IP地址不正确!")
$sIPAddress = $tempArray[0]&"."&$tempArray[1]&"."&$tempArray[2]&"."&$tempArray[3]
$iPort = GUICtrlRead($inputPort)
If $iPort<0 Or $iPort>65535 Then Return MsgBox(48+262144+8192,"警告","端口不正确!")
IniWrite($cfgFile,"Set",$serverIpCfgName,$sIPAddress)
IniWrite($cfgFile,"Set",$serverPortCfgName,$iPort)
Local $account=GUICtrlRead($inputAccount)
Local $password1=GUICtrlRead($inputPwd1)
Local $password2=GUICtrlRead($inputPwd2)
If $account="" Or $password1="" Or $password2="" Or StringCompare($password1,$password2)<>0 Then Return MsgBox(16,"错误","账号密码输入有误!")
$account = Binary($account)
$password1 = Binary($password1)
$password2 = Binary($password2)
ConsoleWrite($account&@CRLF&$password1&@CRLF&$password2&@CRLF&"REGISTE"&$fengefu&$account&$fengefu&$password1&$fengefu&$password2&@CRLF)
TCPStartup()
Local $iSocket = TCPConnect($sIPAddress, $iPort)
If @error Then
ConsoleWrite("Can not connect..."&@CRLF)
TCPCloseSocket($iSocket)
TCPShutdown()
Return False
EndIf
;此处发送时 应对原始内容加密。 密码一般用MD5加密后发送到服务器
TCPSend($iSocket, "REGISTE"&$fengefu&$account&$fengefu&$password1&$fengefu&$password2)
If @error Then
ConsoleWrite("Can not send..."&@CRLF)
TCPCloseSocket($iSocket)
TCPShutdown()
Return False
EndIf
Do
Local $sReceived = TCPRecv($iSocket, 256)
Until $sReceived<>""
ConsoleWrite("Recived: "&BinaryToString($sReceived,4)&@CRLF)
; 关闭套接字.
TCPCloseSocket($iSocket)
TCPShutdown()
If $sReceived<>"" Then
If StringCompare($sReceived , $REGISTESUCESS)=0 Then
MsgBox(64,"成功","注册成功!")
ElseIf StringCompare($sReceived , $REGISTEFAIL)=0 Then
MsgBox(48,"失败","注册失败!")
EndIf
EndIf
EndFunc
Func formRegisteClose()
Exit
EndFunc
Func CheckIpAddress($_ipArray)
If $_ipArray[0]>255 Or $_ipArray[1]>255 Or $_ipArray[2]>255 Or $_ipArray[3]>255 Or (Int($_ipArray[0])+Int($_ipArray[1])+Int($_ipArray[2])+Int($_ipArray[3]))=0 Then
Return False
Else
Return True
EndIf
EndFunc
|