Hallo @all!
Da ich mir mal ein ChatProgramm mit allen Schikanen machen wollte, war das Verschicken von Dateien unverzichtbar.
Dazu hab ich mir 2 Funktionen gemacht
- _TCP_FileSend
- _TCP_FileRecv
Vielleicht kann sie ja jemand brauchen...
UDF:
; #FUNCTION# ======================================================================================
; Name ..........: _TCP_FileRecv()
; Description ...: This function recives a file and save it on a user defined path.
; Syntax ........: _TCP_FileRecv($iSocket, $sSavePath)
; Parameters ....: $iSocket - The connected socket
; $sSavePath - The path you want to save the file
; Return values .: Success Return 1
; Failure:
; |-1: Socket = -1
; |-2: SavePath = ""
; |-3: SavePath does not exist
; |-4: No data was incoming
; |-5: Incoming data wasn´t a file
; Author ........: GE IXI TIM
; Remarks .......: This function can only be used with _TCP_FileSend.
; Link ..........: http://www.autoit.de
; Example .......:
; =================================================================================================
Func _TCP_FileRecv($iSocket, $sSavePath)
Local $hFile, $sRecv
If $iSocket = -1 Then Return -1
If $sSavePath = "" Then Return -2
If StringRight($sSavePath, 1) <> "\" Then $sSavePath &= "\"
If Not FileExists($sSavePath) Then Return -3
$sRecv = TCPRecv($iSocket, 999999)
If $sRecv <> "" Then
$aRecv = StringSplit($sRecv, "<||>", 1)
if $aRecv[1] <> "FILE" Then Return -5
$hFile = FileOpen($sSavePath & $aRecv[2], 18)
FileWrite($hFile, $aRecv[3])
Return 1
EndIf
Return -4
EndFunc
; #FUNCTION# ======================================================================================
; Name ..........: _TCP_FileSend()
; Description ...: This function sends a file
; Syntax ........: _TCP_FileSend($iSocket, $sFilePath[, $sFileName = ""])
; Parameters ....: $iSocket - The connected Socket
; $sFilePath - The path to the file
; $sFileName - [optional] Name of the file. ("" = Default filename) (default:"")
; Return values .: Return 1
; Failure:
; |-1: Socket = -1
; |-2: FilePath does not exist
; |-3: Error with TcpSend
; Author ........: GE IXI TIM
; Remarks .......: This function can only used with _TCP_FileRecv
; Link ..........: http://www.autoit.de
; Example .......:
; =================================================================================================
Func _TCP_FileSend($iSocket, $sFilePath, $sFileName = "")
Local $hFile, $sFile
If $iSocket = -1 Then Return -1
If Not FileExists($sFilePath) Then Return -2
If $sFileName = "" Then
$aSplit = StringSplit($sFilePath, "\")
$sFileName = $aSplit[$aSplit[0]]
EndIf
$hFile = FileOpen($sFilePath, 16)
$sFile = FileRead($hFile)
FileClose($hFile)
TCPSend($iSocket, "FILE<||>" & $sFileName & "<||>" & $sFile)
If @error Then Return -3
Return 1
EndFunc
Beispiel:
;----Server, start me first----
#include "_TcpFile.au3"
Global $ip = @IPAddress1, $port = 11111, $sucess = 0
TCPStartup()
Global $MainSocket = TCPListen($ip, $port), $conSocket = -1
While $conSocket = -1
$conSocket = TCPAccept($MainSocket)
WEnd
MsgBox(64, "Connection created", "")
While 1
$sucess = _Tcp_FileRecv($conSocket, @DesktopDir)
if $sucess = 1 Then
TCPShutdown()
Exit
EndIf
Sleep(10)
WEnd
;--Client
#include "_TcpFile.au3"
Global $ip = @IPAddress1, $port = 11111, $ConSocket = -1
TCPStartup()
While $ConSocket = -1
$ConSocket = TCPConnect($ip, $port)
WEnd
$file = FileOpenDialog("Choose a File", @DesktopDir, "(*.*)")
_Tcp_FileSend($ConSocket, $file, "")
Sleep(5000)
TCPShutdown()
Exit
ganzes Paket: autoit.de/wcf/attachment/9350/
lg. tim