Ticket #2513: client.au3

File client.au3, 2.8 KB (added by FireFox, 6 years ago)
Line 
1; I am the client, start me after the server!
2
3#include <MsgBoxConstants.au3>
4#include <FileConstants.au3>
5
6Example()
7
8Func Example()
9    TCPStartup()
10
11    OnAutoItExitRegister("OnAutoItExit")
12
13    Local $sIPAddress = "127.0.0.1"
14    Local $iPort = 65432
15
16    Local $iSocket = TCPConnect($sIPAddress, $iPort)
17    Local $iError = 0
18
19    If @error Then
20        $iError = @error
21        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not connect, Error code: " & $iError)
22        Return False
23    EndIf
24
25    Local $hFile = 0
26
27    Local Const $i4KiB = 4096, $bEOF = Binary(@CRLF & "{EOF}"), $iEOFLen = BinaryLen($bEOF)
28
29    Local $bData = ""
30
31    Local $iDataLen = 0
32
33    Local $fEOFReached = False
34
35    #region Added
36    Local $bData2 = ""
37
38    Local $aFilePath[3] = ["test1[2].txt", "test2[2].txt", "test3[2].txt"]
39
40    For $i = 0 To UBound($aFilePath) -1
41        #endregion Added
42        $hFile = FileOpen($aFilePath[$i], BitOR($FO_BINARY, $FO_OVERWRITE))
43
44        Do
45            #region Added
46            If $bData2 <> "" Then
47                $bData = $bData2
48                $bData2 = ""
49            Else
50                #endregion Added
51                $bData = TCPRecv($iSocket, $i4KiB, 1)
52
53                If @error Then
54                    $iError = @error
55                    MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Connection lost, Error code: " & $iError)
56                    Return False
57                EndIf
58            EndIf
59            $iDataLen = BinaryLen($bData)
60
61            If $iDataLen = 0 Then ContinueLoop
62
63            If BinaryMid($bData, 1 + $iDataLen - $iEOFLen, $iEOFLen) = $bEOF Then
64                $bData = BinaryMid($bData, 1, $iDataLen - $iEOFLen)
65
66                $fEOFReached = True
67            EndIf
68
69            FileWrite($hFile, $bData)
70        Until $fEOFReached
71
72        FileClose($hFile)
73
74        TCPSend($iSocket, @CRLF & "{EOFOK}") ; Tell the server the file is successfuly received.
75FileWriteLine("toto.txt", "[" & @MIN & ":" & @MSEC & "]client: send EOFOK")
76
77        Do
78            $bData2 = TCPRecv($iSocket, $i4KiB, 1)
79            ; If an error occured - the server is not sending more files - display a message and return True
80            If @error Then
81FileWriteLine("toto.txt", "[" & @MIN & ":" & @MSEC & "]client: error?")
82
83                TCPCloseSocket($iSocket)
84                MsgBox($MB_SYSTEMMODAL, "", "Client:" & @CRLF & "Files received.")
85                Return True
86            EndIf
87
88            Sleep(10) ; Avoid high CPU usage.
89        Until BinaryLen($bData2) > 0
90
91        ; Reset variables that needs to be reseted.
92        $fEOFReached = False
93    Next
94
95    TCPCloseSocket($iSocket)
96EndFunc   ;==>Example
97
98Func OnAutoItExit()
99    TCPShutdown()
100EndFunc   ;==>OnAutoItExit