#include <Array.au3>
Local $ConnectedSocket, $szData
;指定服务端的IP地址
Local $szIPADDRESS = @IPAddress1
;指定服务端的端口
Local $nPORT = 65432
Local $hGUI = GUICreate('')
While 1
$message = "选择要执行的命令:"
$openfile = FileOpenDialog($message, @WorkingDir & "", "所有支持的文件 (*.txt)", 1, '', $hGUI)
If @error Then
Exit
EndIf
$szData = FileRead($openfile)
Local $Res = StringRegExp($szData, '(?s)(?i)([^\r\n]+)(?:\r?\n)(.+)', 3)
If @error Then
MsgBox(0, '', '读取格式错误', '', $hGUI)
Exit
EndIf
If UBound($Res) <> 2 Then
MsgBox(0, '', '读取格式错误', '', $hGUI)
Exit
EndIf
Local $aRes = _TCPSendData($szData)
Switch StringLower($Res[0])
Case 'backup';下载文件
If IsArray($aRes) Then
DirCreate(@WorkingDir & "\DBback")
Local $savefile = FileOpenDialog('指定文件存放位置', @WorkingDir & "\DBback", "所有支持的文件 (*.mdb)", 1 + 2 + 8, $aRes[1], $hGUI)
If Not @error Then
Local $rFile = FileOpen($savefile, 2 + 8 + 16)
_TCPRecvFile($rFile, $aRes[0])
If Not @error Then MsgBox(0, '', '下载完成')
EndIf
EndIf
Case Else
If IsArray($aRes) Then
_ArrayDisplay($aRes)
Else
MsgBox(0, '执行结果', $aRes, '', $hGUI)
EndIf
EndSwitch
WEnd
Func _TCPSendData($str)
If $str = '' Then Return SetError(1, 0, False)
TCPStartup()
$ConnectedSocket = -1
$ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)
If @error Then
MsgBox(0, "", "TCP连接失败,服务端未启用!错误代码: " & @error, '', $hGUI)
Return SetError(2, 0, False)
EndIf
Local $szData = $str
$szData = StringToBinary($szData & Chr(4), 4)
TCPSend($ConnectedSocket, $szData)
Sleep(10)
Local $string
While $string = ''
$string = _TCPRecv()
WEnd
Local $aRes = _DeConversion($string)
If Not @error Then
Select
Case $aRes[0] == 'error'
TCPShutdown()
MsgBox(0, '发生错误', $aRes[1], '', $hGUI)
Return SetError(3, 0, False)
Case $aRes[0] == 'query'
TCPShutdown()
Return SetError(0, 0, $aRes[1])
Case $aRes[0] == 'update'
TCPShutdown()
Return SetError(0, 0, $aRes[1])
Case $aRes[0] == 'backup'
Local $get = StringSplit($aRes[1], '|', 2)
Return SetError(0, 0, $get)
EndSelect
Else
TCPShutdown()
MsgBox(0, '', '读取数据失败,请重试', '', $hGUI)
Return SetError(4, 0, False)
EndIf
EndFunc ;==>_TCPSendData
Func _DeConversion($str)
Local $Return[2], $Res, $tempArray
$Res = StringRegExp($str, '(?s)(?i)([^\r?\n]+)(?:\r?\n)([^\r?\n]+)(?:\r?\n)(.+)', 3)
If @error Then Return SetError(1, 0, False)
If UBound($Res) <> 3 Then Return SetError(2, 0, False)
Switch $Res[0]
Case 'query'
If $Res[1] == 'error' Then
$Return[0] = 'error'
$Return[1] = '不能正确显示结果,无可用分隔符,请联系作者' & @CRLF & @CRLF & $Res[2]
Else
$tempArray = _StringToArray2D($Res[2], $Res[1])
If @error Then Return SetError(3, 0, False)
$Return[0] = 'query'
$Return[1] = $tempArray
EndIf
Case 'update'
$Return[0] = 'update'
$Return[1] = $Res[2]
Case 'error'
$Return[0] = 'error'
$Return[1] = $Res[2]
Case 'backup'
$Return[0] = 'backup'
$Return[1] = $Res[1] & '|' & $Res[2]
Case Else
Return SetError(4, 0, False)
EndSwitch
Return SetError(0, 0, $Return)
EndFunc ;==>_DeConversion
Func _StringToArray2D($data, $sDelim = "|")
Local $temp, $string, $k
$string = $data
$temp = StringSplit($string, $sDelim, 1)
Local $array2d[$temp[1]][$temp[2]]
$k = 3
For $i = 0 To $temp[1] - 1
For $j = 0 To $temp[2] - 1
If UBound($temp) > $k Then
$array2d[$i][$j] = $temp[$k]
$k += 1
Else
Return SetError(1, 0, 0)
EndIf
Next
Next
Return SetError(0, 0, $array2d)
EndFunc ;==>_StringToArray2D
Func _TCPRecv()
Local $recv, $string = ''
While 1
$recv = TCPRecv($ConnectedSocket, 2048, 1)
If $recv = '' Then ExitLoop
If $recv <> '' Then $string &= BinaryToString($recv, 4)
WEnd
Return $string
EndFunc ;==>_TCPRecv
Func _TCPRecvFile($rFile, $rFileSize)
If $rFile = '' Then Return SetError(1, 0, False)
If $rFileSize <= 0 Then Return SetError(2, 0, False)
TCPStartup()
$ConnectedSocket = -1
$ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)
If @error Then
MsgBox(0, "", "TCP连接失败,服务端未启用!错误代码: " & @error, '', $hGUI)
Return SetError(3, 0, False)
EndIf
Local $szData = 'get' & @CRLF & ' '
$szData = StringToBinary($szData & Chr(4), 4)
TCPSend($ConnectedSocket, $szData)
Sleep(10)
Local $rData, $ri = 0;字节计数器归零
While 1
$rData = TCPRecv($ConnectedSocket, 1024 * 1024, 1);开始接收二进制文件数据
If Not @error Then
$ri += BinaryLen($rData);已读取字节计数器
ToolTip('已下载字节:' & $ri)
FileWrite($rFile, $rData);写入文件
Else
Return SetError(1, 0, False)
EndIf
If $ri = $rFileSize Then ExitLoop;计数器与文件实际字节相符退出到主循环
WEnd
FileClose($rFile);关闭文件句柄
Return SetError(0, 0, True)
EndFunc ;==>_TCPRecvFile
请用以上代码测试