_TCPFileTransfer.au3


SUBMITTED BY: Guest

DATE: May 28, 2013, 8:22 a.m.

FORMAT: Text only

SIZE: 5.1 kB

HITS: 1023

  1. #include <WinAPI.au3>
  2. #cs ----------------------------------------------------------------------------
  3. AutoIt Version: 3.3.6.0
  4. Author: DELmE
  5. Function:
  6. _TCPFileRecv
  7. Description:
  8. Receives a file being sent over a TCP connection
  9. Parameters:
  10. sock The TCP socket to be used
  11. rFile The name of the file to save to
  12. offset = 0 The offset to start writing data to the file at (to be used if the transfer is somehow interrupted) default value is 0 (no offset)
  13. $DataSize = 1024 The number of bytes of data to be received per packet, default value is 1024
  14. Return value:
  15. Success _TCPFileRecv returns the number of bytes written to the file
  16. Failure _TCPFileRecv returns 0 if the function failed and sets @error to the following:
  17. 1 = Error opening the file
  18. 2 = Failed to set offset
  19. 3 = Failed to receive data on socket (invalid socket)
  20. 4 = Unable to write data to the file
  21. Also sets @extended to the number of bytes of data that was successfully written to the file
  22. #ce ----------------------------------------------------------------------------
  23. Func _TCPFileRecv($sock, $rFile, $offset = 0, $DataSize = 1024)
  24. Local $i, $x, $lpszBuff, $nBytes, $rBuff, $hFile
  25. $nBytes = 0
  26. $lpszBuff = DllStructCreate("byte["&$DataSize&"]")
  27. $hFile = _WinAPI_CreateFile($rFile,3,4,4)
  28. If $hFile = 0 Then
  29. _WinAPI_CloseHandle($hFile)
  30. SetError(1)
  31. SetExtended($nBytes)
  32. Return 0
  33. EndIf
  34. While 1
  35. If _WinAPI_SetFilePointer($hFile, $offset, 0) = -1 Then
  36. _WinAPI_CloseHandle($hFile)
  37. SetError(2)
  38. SetExtended($nBytes)
  39. Return 0
  40. EndIf
  41. $rBuff = ""
  42. $rBuff = TCPRecv($sock, $DataSize)
  43. If $rBuff <> "" Then
  44. If @error = -1 Then
  45. _WinAPI_CloseHandle($hFile)
  46. SetError(3)
  47. SetExtended($nBytes)
  48. Return 0
  49. EndIf
  50. If $rBuff = @CRLF&@CRLF Then
  51. ExitLoop
  52. Else
  53. DllStructSetData($lpszBuff, 1, $rBuff)
  54. $x = StringLen(BinaryToString($rBuff))
  55. If _WinAPI_WriteFile($hFile, DllStructGetPtr($lpszBuff), $x, $i) = False Then
  56. _WinAPI_CloseHandle($hFile)
  57. SetError(4)
  58. SetExtended($nBytes)
  59. Return 0
  60. Else
  61. $nBytes += $i
  62. $offset += $i
  63. EndIf
  64. EndIf
  65. EndIf
  66. WEnd
  67. _WinAPI_CloseHandle($hFile)
  68. SetExtended($nBytes)
  69. Return $nBytes
  70. EndFunc ;==> _TCPFileRecv
  71. #cs ----------------------------------------------------------------------------
  72. AutoIt Version: 3.3.6.0
  73. Author: DELmE
  74. Function:
  75. _TCPFileSend
  76. Description:
  77. Sends a file over a TCP connection
  78. Parameters:
  79. sock The tcp socket to be used
  80. sFile The file to be sent
  81. offset = 0 The offset to start reading data from the file at (to be used if the transfer is somehow interrupted) default value is 0 (no offset)
  82. $DataSize = 1024 The number of bytes of data to be sent per packet, default value is 1024
  83. Return value:
  84. Success _TCPFileSend returns the number of bytes sent
  85. Failure _TCPFileSend returns 0 if an error occurred and sets @error to the following:
  86. 1 = The file does not exist
  87. 2 = Failed to set offset
  88. 3 = Failed to read data from file
  89. 4 = Failed to send data through socket
  90. Also sets @extended to the number of bytes of data that was successfully sent
  91. #ce ----------------------------------------------------------------------------
  92. Func _TCPFileSend($sock, $sFile, $offset = 0, $DataSize = 1024)
  93. Local $i, $lpszBuff, $nBytes, $sBuff, $hFile, $nFileSize
  94. $nFileSize = FileGetSize($sFile)
  95. $nFileSize -= $offset
  96. If $nFileSize < $DataSize Then $DataSize = $nFileSize
  97. $nBytes = 0
  98. $lpszBuff = DllStructCreate("byte["&$DataSize&"]")
  99. $hFile = _WinAPI_CreateFile($sFile,2,2,2)
  100. If $hFile = 0 Then
  101. _WinAPI_CloseHandle($hFile)
  102. SetError(1)
  103. SetExtended($nBytes)
  104. Return 0
  105. EndIf
  106. While $nFileSize > 0
  107. If _WinAPI_SetFilePointer($hFile, $offset, 0) = -1 Then
  108. _WinAPI_CloseHandle($hFile)
  109. SetError(2)
  110. SetExtended($nBytes)
  111. Return 0
  112. EndIf
  113. DllStructSetData($lpszBuff,1,"")
  114. If _WinAPI_ReadFile($hFile, DllStructGetPtr($lpszBuff), $DataSize, $i) = False Then
  115. _WinAPI_CloseHandle($hFile)
  116. SetError(3)
  117. Return 0
  118. Else
  119. $sBuff = DllStructGetData($lpszBuff, 1)
  120. $i = TCPSend($sock, $sBuff)
  121. If $i = 0 Then
  122. _WinAPI_CloseHandle($hFile)
  123. SetError(4)
  124. SetExtended($nBytes)
  125. Return 0
  126. Else
  127. $nBytes += $i
  128. $offset += $i
  129. $nFileSize -= $i
  130. EndIf
  131. EndIf
  132. WEnd
  133. _WinAPI_CloseHandle($hFile)
  134. TCPSend($sock, @CRLF&@CRLF)
  135. SetExtended($nBytes)
  136. Return $nBytes
  137. EndFunc ;==> _TCPFileSend

comments powered by Disqus