| 1 | ; I am the server, start me first!
|
|---|
| 2 |
|
|---|
| 3 | #include <MsgBoxConstants.au3>
|
|---|
| 4 | #include <FileConstants.au3>
|
|---|
| 5 |
|
|---|
| 6 | Example()
|
|---|
| 7 |
|
|---|
| 8 | Func Example()
|
|---|
| 9 | TCPStartup()
|
|---|
| 10 |
|
|---|
| 11 | OnAutoItExitRegister("OnAutoItExit")
|
|---|
| 12 |
|
|---|
| 13 | Local $iError = 0
|
|---|
| 14 |
|
|---|
| 15 | Local $sIPAddress = "127.0.0.1"
|
|---|
| 16 | Local $iPort = 65432
|
|---|
| 17 |
|
|---|
| 18 | Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
|
|---|
| 19 |
|
|---|
| 20 | If @error Then
|
|---|
| 21 | $iError = @error
|
|---|
| 22 | MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not listen, Error code: " & $iError)
|
|---|
| 23 | Return False
|
|---|
| 24 | EndIf
|
|---|
| 25 |
|
|---|
| 26 | Local $iSocket = 0
|
|---|
| 27 |
|
|---|
| 28 | Do
|
|---|
| 29 | $iSocket = TCPAccept($iListenSocket)
|
|---|
| 30 |
|
|---|
| 31 | If @error Then
|
|---|
| 32 | $iError = @error
|
|---|
| 33 | MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not accept the incoming connection, Error code: " & $iError)
|
|---|
| 34 | Return False
|
|---|
| 35 | EndIf
|
|---|
| 36 | Until $iSocket <> -1
|
|---|
| 37 |
|
|---|
| 38 | TCPCloseSocket($iListenSocket)
|
|---|
| 39 |
|
|---|
| 40 | Local $iFileSize = 0, $hFile = 0, $iOffset = 0, $bData = ""
|
|---|
| 41 | Local Const $i4KiB = 4096
|
|---|
| 42 |
|
|---|
| 43 | #region Added
|
|---|
| 44 | Local $aFilePath[3] = ["test1.txt", "test2.txt", "test3.txt"]
|
|---|
| 45 |
|
|---|
| 46 | For $i = 0 To UBound($aFilePath) - 1
|
|---|
| 47 | #endregion Added
|
|---|
| 48 | $iFileSize = FileGetSize($aFilePath[$i])
|
|---|
| 49 |
|
|---|
| 50 | $hFile = FileOpen($aFilePath[$i], $FO_BINARY)
|
|---|
| 51 |
|
|---|
| 52 | Do
|
|---|
| 53 | FileSetPos($hFile, $iOffset, $FILE_BEGIN)
|
|---|
| 54 |
|
|---|
| 55 | TCPSend($iSocket, FileRead($hFile, $i4KiB))
|
|---|
| 56 |
|
|---|
| 57 | If @error Then
|
|---|
| 58 | $iError = @error
|
|---|
| 59 | TCPCloseSocket($iSocket)
|
|---|
| 60 | MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not send the data, Error code: " & $iError)
|
|---|
| 61 | Return False
|
|---|
| 62 | EndIf
|
|---|
| 63 |
|
|---|
| 64 | $iOffset += $i4KiB
|
|---|
| 65 | Until $iOffset >= $iFileSize
|
|---|
| 66 |
|
|---|
| 67 | FileClose($hFile)
|
|---|
| 68 |
|
|---|
| 69 | TCPSend($iSocket, @CRLF & "{EOF}")
|
|---|
| 70 | FileWriteLine("toto.txt", "[" & @MIN & ":" & @MSEC & "]serv: send EOF")
|
|---|
| 71 |
|
|---|
| 72 | ;~ MsgBox($MB_SYSTEMMODAL, "", "Server:" & @CRLF & "File sent.")
|
|---|
| 73 |
|
|---|
| 74 | #region Added
|
|---|
| 75 | Do
|
|---|
| 76 | $bData = TCPRecv($iSocket, 9, 1) ; 9 = Length of: @CRLF{EOFOK}
|
|---|
| 77 | If @error Then
|
|---|
| 78 | $iError = @error
|
|---|
| 79 | FileWriteLine("toto.txt", "[" & @MIN & ":" & @MSEC & "]serv: error")
|
|---|
| 80 | MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Connection lost, Error code: " & $iError)
|
|---|
| 81 | Return False
|
|---|
| 82 | EndIf
|
|---|
| 83 |
|
|---|
| 84 | If BinaryLen($bData) = 0 Then
|
|---|
| 85 | Sleep(10) ; Avoid High CPU usage.
|
|---|
| 86 | ContinueLoop
|
|---|
| 87 | EndIf
|
|---|
| 88 | Until BinaryToString($bData) = @CRLF & "{EOFOK}"
|
|---|
| 89 | FileWriteLine("toto.txt", "[" & @MIN & ":" & @MSEC & "]serv: recv EOFOK")
|
|---|
| 90 |
|
|---|
| 91 | ; Reset variables that needs to be reseted.
|
|---|
| 92 | $iOffset = 0
|
|---|
| 93 | Next
|
|---|
| 94 |
|
|---|
| 95 | TCPSend($iSocket, @CRLF & "{EOT}") ; EOT stands for End Of Transfer (this is a code, choose what you want).
|
|---|
| 96 | #endregion Added
|
|---|
| 97 |
|
|---|
| 98 | TCPCloseSocket($iSocket)
|
|---|
| 99 | EndFunc ;==>Example
|
|---|
| 100 |
|
|---|
| 101 | Func OnAutoItExit()
|
|---|
| 102 | TCPShutdown()
|
|---|
| 103 | EndFunc ;==>OnAutoItExit
|
|---|