Sign in to follow this  
Followers 0
Tektodon

Filesending via TCP

3 posts in this topic

Hey there,

is it possible to send whole files via the TCP protocol?

Already tried a lot of stuff, but I can only send text...

Please help me, gettin' mad this time!! :Þ

Share this post


Link to post
Share on other sites



Here is an example from someone on the forums I got working. Rough code, don't trust it to be 100% but tested and works:

Client

CODE

;-Top calls

#NoTrayIcon

;includes

#include <Misc.au3>

#include <GUIConstants.au3>

;

opt("TrayIconHide", 1)

opt("GUIOnEventMode", 1)

;

;-Variables

$Ip = @IPAddress1

$Port = 31337

$defaultport = 31337

$packsize = 4096

$error = 0

Dim $recvbuffer = ""

$lastmessage = ""

$connected = 0

$lasttimepack = TimerInit()

$lastdiffpack = -1

$bytessend = 0

$timer1 = TimerInit()

$transferspeedvar = ""

$version = "1.1"

$err = 0

$HeaderOk = 0

$packetssend = 0

$concheck = 0

$concheck2 = 0

Dim $buttonbrowse, $buttonsend, $editip, $editport, $editfile, $progress1, $labelstatus, $labelprogress, $labellastmsg, $ffullname, $fname, $fsize, $Connect, $filehand

;

;-Functions

TCPStartup()

$dll = DllOpen("user32.dll")

$mainhandle = MakeWindow($buttonbrowse, $buttonsend, $editip, $editfile, $progress1, $labelstatus, $labelprogress, $labellastmsg, $editport)

GUICtrlSetData($editip, @IPAddress1)

;-OnEvent Definitions (Remember to check if window was created before doing this)

GUISetOnEvent($GUI_EVENT_CLOSE, "Exitme", $mainhandle)

GUICtrlSetOnEvent($buttonbrowse, "Browse")

GUICtrlSetOnEvent($buttonsend, "Sendbutton")

;

GUICtrlSetData($editip, $Ip)

GUICtrlSetData($editport, $Port)

;

;Main Loop

While 1

If $connected = 1 Then

If $concheck2 = 1 Then

GUICtrlSetState($editfile, $Gui_disable)

GUICtrlSetState($editip, $Gui_disable)

GUICtrlSetState($editport, $Gui_disable)

GUICtrlSetState($buttonbrowse, $Gui_disable)

;GuiCtrlSetState($buttonsend,$Gui_disable)

GUICtrlSetData($buttonsend, "Stop!")

$concheck2 = 0

EndIf

If $concheck = 0 Then $concheck = 1

Sleep(1)

If TimerDiff($timer1) > 250 Then

$transferspeedvar = Transferspeed($lastdiffpack, $packsize)

$timer1 = TimerInit()

EndIf

GUICtrlSetData($progress1, roundup(100 * (($packetssend * $packsize) / $fsize)))

GUICtrlSetData($labelprogress, StringFormat("% 6.1f", Round(100* (($packetssend * $packsize) / $fsize), 1)) & " % of " & bytestring($fsize) & " at " & $transferspeedvar)

GUICtrlSetData($labelstatus, "Connected");"Connected"

If $err <> 0 Then GUICtrlSetData($labelstatus, "Not Connected")

$recvbuffer = $recvbuffer & TCPRecv($Connect, 4096)

$err = @error

If $err <> 0 Then $connected = 0

If StringInStr($recvbuffer, "!") Then

$lastmessage = StringRegExpReplace($recvbuffer, ".*?[!]?([^!]+!\>)", "\1")

$recvbuffer = ""

EndIf

if (NOT ($HeaderOk = 1)) Then

$checkvar = CheckHeader($lastmessage, $packsize, $version)

;msgbox(0,"",$packsize)

If $checkvar = 1 Then

TCPSend($Connect, "Header:" & $fname & ";" & $fsize & ";HeaderEnd")

Else

If $checkvar = -1 Then TCPSend($Connect, "VersionConflict!")

EndIf

EndIf

If $lastmessage == "HeaderOk!" Then

$HeaderOk = 1

$lastmessage = ""

GUICtrlSetData($labellastmsg, "File accepted - Transferring...")

if (Int($fsize / $packsize)) >= ($packetssend) Then

$bytessend = TCPSend($Connect, FileRead($filehand, $packsize))

Else

$bytessend = TCPSend($Connect, FileRead($filehand, $fsize - Int($packsize * $packetssend)))

EndIf

EndIf

If $lastmessage == "Nextpart!" Then

$packetssend += 1

$lastmessage = ""

$lastdiffpack = TimerDiff($lasttimepack)

$lasttimepack = TimerInit()

if (Int($fsize / $packsize)) >= ($packetssend) Then

$bytessend = TCPSend($Connect, FileRead($filehand, $packsize))

Else

$bytessend = TCPSend($Connect, FileRead($filehand, $fsize - Int($packsize * $packetssend)))

EndIf

EndIf

If $lastmessage = "TransferOk!" Then

GUICtrlSetState($progress1, $Gui_hide)

GUICtrlSetData($labellastmsg, "Transfer Complete")

GUICtrlSetData($labelstatus, "Not Connected")

$lastmessage = ""

$connected = 0

$packetssend = 0

EndIf

If $lastmessage = "Bye!" Then

GUICtrlSetState($progress1, $Gui_hide)

GUICtrlSetData($labellastmsg, "Version Conflict")

$lastmessage = ""

$connected = 0

$packetssend = 0

EndIf

If $lastmessage = "Serverclosed!" Then

GUICtrlSetState($progress1, $Gui_hide)

GUICtrlSetData($labellastmsg, "Server Closed")

$lastmessage = ""

$connected = 0

$packetssend = 0

EndIf

Else

Sleep(250)

If $concheck2 <> 1 Then $concheck2 = 1

If $concheck <> 0 Then

FileClose($filehand)

$concheck = 0

$lastmessage = ""

$connected = 0

$packetssend = 0

GUICtrlSetState($progress1, $Gui_hide)

GUICtrlSetState($editfile, $Gui_enable)

GUICtrlSetState($editip, $Gui_enable)

GUICtrlSetState($editport, $Gui_enable)

GUICtrlSetState($buttonbrowse, $Gui_enable)

;GuiCtrlSetState($buttonsend,$Gui_enable)

GUICtrlSetData($buttonsend, "Send")

GUICtrlSetData($labelstatus, "Not Connected")

GUICtrlSetData($labelprogress, "")

EndIf

EndIf

WEnd

;

;Functions Definitions

Func Transferspeed($timerdiff, $packetsize)

If $timerdiff <> - 1 Then

Return bytestring(($packetsize / ($timerdiff / 1000))) & "/s"

Else

Return "0Kb/s"

EndIf

EndFunc ;==>Transferspeed

Func bytestring($inputbytes)

If $inputbytes < 1024 Then Return Round($inputbytes) & "b"

If $inputbytes > 1024 And $inputbytes < (1024 * 1024) Then Return Round($inputbytes / 1024, 1) & " Kb"

If $inputbytes > (1024 * 1024) And $inputbytes < (1024 * 1024 * 1024) Then Return Round($inputbytes/ (1024 * 1024), 1) & " Mb"

If $inputbytes > (1024 * 1024 * 1024) Then Return Round($inputbytes/ (1024 * 1024 * 1024), 1) & " Gb"

EndFunc ;==>bytestring

Func roundup($inputvar)

If Int($inputvar) < $inputvar Then

Return Number(Int($inputvar) + 1)

Else

Return Int($inputvar)

EndIf

EndFunc ;==>roundup

Func CheckHeader($RecvString, ByRef $packetsize, $version1)

Local $RegExp1 = "Hi\{([0-9]{1,21}\.[0-9]{1,21});([0-9]{1,255})\}!"

If StringRegExp($RecvString, $RegExp1) Then

$Header = StringRegExp($RecvString, $RegExp1, 1)

If UBound($Header) = 2 Then

$packetsize = Int(Number($Header[1]))

If $Header[0] <> $version1 Then

Return -1

Else

Return 1

EndIf

Else

Return 0

EndIf

Else

Return 0

EndIf

EndFunc ;==>CheckHeader

Func Browse()

Global $fname, $ffullname, $editfile

$ffullname = FileOpenDialog("Choose File", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "All Files(*.*)", 3)

$fname = StringSplit($ffullname, "\")

$fname = $fname[$fname[0]]

GUICtrlSetData($editfile, $ffullname)

EndFunc ;==>Browse

Func Sendbutton()

Global $connected, $fname, $ffullname, $fsize, $HeaderOk, $err, $connected, $lastmessage, $recvbuffer, $Connect, $filehand, $Ip, $Port, $labellastmsg, $packetssend, $progress1, $editip, $buttonsend, $editport

If $connected = 0 Then

$recvbuffer = ""

$lastmessage = ""

$err = 0

$HeaderOk = 0

$fsize = 0

$packetssend = 0

$Ip = GUICtrlRead($editip)

$ffullname = GUICtrlRead($editfile)

$readportvar = Int(Number(GUICtrlRead($editport)))

if (IsNumber($readportvar) = 1 And $readportvar < 65536) And $readportvar > 0 Then

$Port = $readportvar

Else

$Port = $defaultport

GUICtrlSetData($editport, $Port)

EndIf

If FileExists($ffullname) = 1 Then

$fsize = FileGetSize($ffullname)

$Ip = TCPNameToIP($Ip)

GUICtrlSetData($editip, $Ip)

$Connect = TCPConnect($Ip, $Port)

If $Connect = -1 Then

GUICtrlSetData($labellastmsg, "Unable to connect")

Else

$connected = 1

GUICtrlSetState($progress1, $Gui_show)

$filehand = FileOpen($ffullname, 0)

EndIf

Else

GUICtrlSetData($labellastmsg, "File does not exist")

EndIf

Else

$connected = 0

GUICtrlSetData($labellastmsg, "User aborted")

TCPCloseSocket($Connect)

EndIf

EndFunc ;==>Sendbutton

Func MakeWindow(ByRef $bbrowse, ByRef $bsend, ByRef $eip, ByRef $efile, ByRef $prog1, ByRef $lstatus, ByRef $lprogress, ByRef $llastmsg, ByRef $eport)

Local $g_height = 100

Local $g_width = 354

Local $progresswidth = 75

Local $statusheight = 18

$winhand1 = GUICreate("Tcp Transfer - Client", $g_width, $g_height, 300, 25)

GUICtrlCreateLabel("Server IP Address: ", 2, 4, 100, 18)

$eip = GUICtrlCreateInput("", 100, 2, 100, 18)

GUICtrlCreateLabel("Port: ", 202, 4, 48, 18)

$eport = GUICtrlCreateInput("", 250, 2, 50, 18)

GUICtrlCreateLabel("File: ", 2, 24, 100, 18)

$efile = GUICtrlCreateInput("", 100, 22, 150, 18)

$bbrowse = GUICtrlCreateButton("Browse", 252, 22, 50, 18)

$bsend = GUICtrlCreateButton("Send", 302, 22, 50, 18)

$lstatus = GUICtrlCreateLabel("Not Connected", 0, $g_height - $statusheight, $g_width / 2, $statusheight, BitOR($SS_SIMPLE, $SS_SUNKEN))

$lprogress = GUICtrlCreateLabel("", $g_width / 2, $g_height - $statusheight, $g_width / 2, $statusheight, BitOR($SS_SIMPLE, $SS_SUNKEN))

GUICtrlCreateLabel("Last Message: ", 2, 45, 200, 18)

$llastmsg = GUICtrlCreateLabel("", 2, 60, $g_width - 4, 18, $SS_SIMPLE)

$prog1 = GUICtrlCreateProgress($g_width / 2 - $progresswidth - 4, $g_height - ($statusheight - 3), $progresswidth, $statusheight - 4)

GUICtrlSetState($prog1, $Gui_hide)

GUISetState(@SW_SHOW, $winhand1)

Return $winhand1

EndFunc ;==>MakeWindow

Func Bitif($inputvar, $inputvar2)

If $inputvar2 = BitAND($inputvar, $inputvar2) Then

Return 1

Else

Return 0

EndIf

EndFunc ;==>Bitif

Func ReadKey($hexkey, $dll1, $handle1)

If _IsPressed ($hexkey, $dll1) And BitAND(WinGetState($handle1), 8) Then

Return 1

Else

Return 0

EndIf

EndFunc ;==>ReadKey

Func CheckLegalFilename($Name1)

Local $lengte = StringLen($Name1)

If $lengte > 255 Then Return 0

Local $RegExp1 = '[^;\\/:*?"<>|]{' & $lengte & '}'

Return StringRegExp($Name1, $RegExp1)

EndFunc ;==>CheckLegalFilename

Func Disconnect($socket1)

TCPCloseSocket($socket1)

EndFunc ;==>Disconnect

Func Exitme()

DllClose($dll)

TCPShutdown()

Exit

EndFunc ;==>Exitme

Func Message($MESSAGE)

MsgBox(64 + 262144, "", $MESSAGE, 3)

Exit 0

EndFunc ;==>Message

;

Server

CODE

;-Top calls

#NoTrayIcon

;includes

#include <Misc.au3>

#include <GUIConstants.au3>

;

opt("TrayIconHide", 1)

opt("GUIOnEventMode", 1)

;

;-Variables

$IP = @IPAddress1

$PORT = 31337

$defaultport = 31337

$Savelocation = @ScriptDir & "\Upload\"

$packsize = 16384

$error = 0

$version = "1.1"

$listenon = 1

$listening = 0

$accepted = 0

$accepton = 0

$headerreceived = 0

$savefileopened = 0

$packetsrecved = 0

$timer1 = 0

$timer2 = 0

$Timer3 = 0

$timer4 = TimerInit()

$completed = 0

$RecvBufferH = ""

Dim $savefname, $savefsize, $savefile, $buttonlisten, $labelstatus, $progress1, $labelinfo, $editport

$socketone = -1

$socketreal = -1

$RecvBufferI = ""

$checkcon = 1

$checkcon2 = 1

$checkcon3 = 1

$infovar = ""

$lasttimepack = TimerInit()

$lastdiffpack = -1

;

;-Functions

DirCreate($Savelocation)

TCPStartup()

$dll = DllOpen("user32.dll")

DirCreate($Savelocation)

$mainhandle = MakeWindow($buttonlisten, $labelstatus, $progress1, $labelinfo, $editport, $PORT)

$label1 = GUICtrlCreateLabel("", 0, 0, 300, 300)

;-OnEvent Definitions (Remember to check if window was created before doing this)

GUISetOnEvent($GUI_EVENT_CLOSE, "Exitme", $mainhandle)

GUICtrlSetOnEvent($buttonlisten, "Listenbutton")

;

;

;

;Main Loop

While 1

If $accepted = 1 Then

Sleep(1)

If $checkcon = 0 Then

$checkcon = 1

GUICtrlSetState($progress1, $GUI_show)

EndIf

Else

If $listening = 1 Then

Sleep(10)

If $checkcon3 = 0 Then

$checkcon3 = 1

GUICtrlSetData($labelinfo, "")

EndIf

Else

If $checkcon3 = 1 Then

$checkcon3 = 0

GUICtrlSetData($labelinfo, "")

EndIf

EndIf

Sleep(250)

GUICtrlSetData($labelinfo, "")

If $checkcon = 1 Then

$checkcon = 0

GUICtrlSetState($progress1, $GUI_hide)

EndIf

EndIf

If $listenon = 1 Then

If $checkcon2 = 0 Then

$checkcon2 = 1

GUICtrlSetData($labelstatus, "Listening")

GUICtrlSetState($editport, $gui_disable)

EndIf

Else

If $checkcon2 = 1 Then

$checkcon2 = 0

GUICtrlSetData($labelstatus, "Server Stopped")

GUICtrlSetState($editport, $gui_enable)

EndIf

EndIf

;If ReadKey("1B",$dll,$mainhandle) Then Exitme()

If $listenon = 1 And $listening = 0 Then

$readportvar = Int(Number(GUICtrlRead($editport)))

if (IsNumber($readportvar) = 1 And $readportvar < 65536) And $readportvar > 0 Then

$PORT = $readportvar

Else

$PORT = $defaultport

GUICtrlSetData($editport, $PORT)

EndIf

GUICtrlSetState($editport, $gui_disable)

$accepton = 0

$socketone = Listen($IP, $PORT)

If $socketone <> - 1 Then $listening = 1

GUICtrlSetData($labelstatus, "Listening")

GUICtrlSetData($buttonlisten, "Stop Server")

Else

If $listenon = 0 Then

If $socketreal <> - 1 Then

TCPSend($socketreal, "ServerClosed!")

Disconnect($socketreal)

FileClose($savefile)

EndIf

$headerreceived = 0

$accepted = 0

$socketreal = -1

$savefileopened = 0

$listening = 0

$accepton = 0

$accepted = 0

TCPCloseSocket($socketone)

$listenon = -1

$timer1 = 0

$timer2 = 0

$Timer3 = 0

EndIf

EndIf

if ($listenon = 1 And $listening = 1) And $socketone <> - 1 Then

$accepton = 1

EndIf

if ($listenon = 1 And $listening = 1) And $socketone = -1 Then $listening = 0

If $accepton = 1 And $accepted = 0 Then

$socketreal = Accept($socketone)

If $socketreal <> - 1 Then $accepted = 1

If $accepted = 1 Then GUICtrlSetData($labelstatus, "Client connected")

$savefname = ""

$savefsize = ""

$savefile = ""

$timer1 = 0

$timer2 = 0

$Timer3 = 0

EndIf

if ($accepton = 1 And $accepted = 1) And $socketreal = -1 Then $accepted = 0

if (($accepton = 1 And $accepted = 1) And $socketreal <> - 1) And $headerreceived = 0 Then

If $timer1 = 0 Then

$timer1 = TimerInit()

$RecvBufferH = ""

EndIf

If $timer2 = 0 Then

$timer2 = TimerInit()

EndIf

$return1 = RecvHeader($socketreal, $Savelocation, $savefname, $savefsize, $packsize, $RecvBufferH)

If $return1 = 1 Then

$headerreceived = 1

GUICtrlSetData($labelstatus, "File accepted")

$RecvBufferH = ""

Else

If $return1 < 1 Then

If $return1 = -3 Then

TCPSend($socketreal, "Bye!")

$accepted = 0

Disconnect($socketreal)

$socketreal = -1

$RecvBufferH = ""

$timer1 = 0

$timer2 = 0

Else

$headerreceived = 0

If TimerDiff($timer2) > 500 Then

TCPSend($socketreal, "Hi{" & $version & ";" & $packsize & "}!") ;

$timer2 = 0

EndIf

If TimerDiff($timer1) > 10000 Then

TCPSend($socketreal, "Error_RecvHeader!")

$accepted = 0

Disconnect($socketreal)

GUICtrlSetData($labelstatus, "Listening")

$socketreal = -1

$RecvBufferH = ""

$timer1 = 0

$timer2 = 0

EndIf

EndIf

Else

$accepted = 0

Disconnect($socketreal)

GUICtrlSetData($labelstatus, "Listening")

$socketreal = -1

$RecvBufferH = ""

$timer1 = 0

$timer2 = 0

EndIf

EndIf

EndIf

if (($accepton = 1 And $accepted = 1) And $socketreal <> - 1) And $headerreceived = 1 Then

If $savefileopened = 0 Then

$savefile = FileOpen($Savelocation & $savefname, 2)

GUICtrlSetData($labelstatus, "Receiving file")

$savefileopened = 1

$packetsrecved = 0

$completed = 0

EndIf

If $savefile <> - 1 Then

$percentr = roundup(100 * (($packetsrecved * $packsize) / $savefsize))

GUICtrlSetData($progress1, $percentr)

If TimerDiff($timer4) > 250 Then

$infovar = "Filename: """ & $savefname & """" & @CRLF & "Filesize: " & bytestring($savefsize) & @CRLF & "Data received: " & bytestring($packetsrecved * $packsize) & @CRLF

$infovar &= "Percent received: " & $percentr & " %" & @CRLF & "Transferspeed: " & Transferspeed($lastdiffpack, $packsize)

GUICtrlSetData($labelinfo, $infovar)

$timer4 = TimerInit()

EndIf

If $savefileopened = 1 Then

If $Timer3 = 0 Then

$Timer3 = TimerInit()

;$RecvBufferI = ""

EndIf

If Int($savefsize / $packsize) = $packetsrecved Then

$lastpackif = $savefsize - (Int($savefsize / $packsize) * $packsize)

Else

$lastpackif = 0

EndIf

$recvreturn = RecvIt($socketreal, $lastpackif, $packsize, $RecvBufferI)

;$testvar = Stringlen($RecvBufferI)

If $recvreturn <> 1 Then

If TimerDiff($Timer3) > 5000 Then

$headerreceived = 0

$accepted = 0

TCPSend($socketreal, "Timed out please send data!")

Disconnect($socketreal)

GUICtrlSetData($labelstatus, "Listening")

FileClose($savefile)

$socketreal = -1

$savefileopened = 0

$Timer3 = 0

EndIf

Else

FileWrite($savefile, $RecvBufferI)

$packetsrecved += 1

$Timer3 = 0

;msgbox(0,"",int($savefsize/$packsize) & " " & $packetsrecved)

If Int($savefsize / $packsize) >= $packetsrecved Then

TCPSend($socketreal, "Nextpart!")

$lastdiffpack = TimerDiff($lasttimepack)

$lasttimepack = TimerInit()

Else

$completed = 1

EndIf

$RecvBufferI = ""

;$testvar=1

EndIf

If $completed = 1 Then

TCPSend($socketreal, "TransferOk!")

;msgbox(0,"",FileGetSize($Savelocation & $savefname))

$completed = 0

$headerreceived = 0

$accepted = 0

;sleep(1000)

Disconnect($socketreal)

GUICtrlSetData($labelstatus, "Listening")

FileClose($savefile)

$socketreal = -1

$savefileopened = 0

EndIf

Else

$headerreceived = 0

$accepted = 0

TCPSend($socketreal, "ServerError1!")

Disconnect($socketreal)

GUICtrlSetData($labelstatus, "Listening")

$socketreal = -1

$savefileopened = 0

EndIf

Else

$headerreceived = 0

$accepted = 0

TCPSend($socketreal, "ServerError2!")

Disconnect($socketreal)

GUICtrlSetData($labelstatus, "Listening")

$socketreal = -1

$savefileopened = 0

EndIf

EndIf

WEnd

;

;Functions Definitions

Func Transferspeed($timerdiff, $packetsize)

If $timerdiff <> - 1 Then

Return bytestring(($packetsize / ($timerdiff / 1000))) & "/s"

Else

Return "0Kb/s"

EndIf

EndFunc ;==>Transferspeed

Func bytestring($inputbytes)

If $inputbytes < 1024 Then Return Round($inputbytes) & "b"

If $inputbytes > 1024 And $inputbytes < (1024 * 1024) Then Return Round($inputbytes / 1024, 1) & " Kb"

If $inputbytes > (1024 * 1024) And $inputbytes < (1024 * 1024 * 1024) Then Return Round($inputbytes/ (1024 * 1024), 1) & " Mb"

If $inputbytes > (1024 * 1024 * 1024) Then Return Round($inputbytes/ (1024 * 1024 * 1024), 1) & " Gb"

EndFunc ;==>bytestring

Func roundup($inputvar)

If Int($inputvar) < $inputvar Then

Return Number(Int($inputvar) + 1)

Else

Return Int($inputvar)

EndIf

EndFunc ;==>roundup

Func Listenbutton()

Global $listenon, $buttonlisten, $socketreal, $socketone

If $listenon = 1 Then

GUICtrlSetData($buttonlisten, "Start server")

$listenon = 0

Else

;GuiCtrlSetData($buttonlisten,"Stop server!")

$listenon = 1

EndIf

EndFunc ;==>Listenbutton

Func Bitif($inputvar, $inputvar2)

If $inputvar2 = BitAND($inputvar, $inputvar2) Then

Return 1

Else

Return 0

EndIf

EndFunc ;==>Bitif

Func ReadKey($hexkey, $dll1, $handle1)

If _IsPressed ($hexkey, $dll1) And BitAND(WinGetState($handle1), 8) Then

Return 1

Else

Return 0

EndIf

EndFunc ;==>ReadKey

Func MakeWindow(ByRef $blisten, ByRef $lstatus, ByRef $prog1, ByRef $linfo, ByRef $eport, $Port1)

Local $g_height = 200

Local $g_width = 300

Local $statusheight = 18

$winhand1 = GUICreate( "Tcp Transfer - Server", $g_width, $g_height, 0, 0)

$blisten = GUICtrlCreateButton("Start server", 75, 2, 150, 18)

GUICtrlCreateLabel("Port: ", 227, 4, 20, 18)

$eport = GUICtrlCreateInput($Port1, 250, 2, 45, 18)

$linfo = GUICtrlCreateLabel("", 2, 24, 296, 130, $SS_Sunken)

$lstatus = GUICtrlCreateLabel("Not Connected", 0, $g_height - $statusheight, $g_width, $statusheight, BitOR($SS_SIMPLE, $SS_Sunken))

$prog1 = GUICtrlCreateProgress(2, 160, $g_width - 4, $statusheight)

GUICtrlSetState($prog1, $GUI_hide)

GUISetState(@SW_SHOW, $winhand1)

Return $winhand1

EndFunc ;==>MakeWindow

Func Sendit()

EndFunc ;==>Sendit

Func RecvIt($socket1, $lastpack, $packetsize1, ByRef $RecvBuffer)

$bytesrecv = StringLen($RecvBuffer)

If $lastpack < 1 Then

$RecvBuffer = $RecvBuffer & TCPRecv($socket1, $packetsize1 - $bytesrecv)

if (StringLen($RecvBuffer) >= $packetsize1) Then

Return 1

Else

Return 0

EndIf

Else

$RecvBuffer = $RecvBuffer & TCPRecv($socket1, $lastpack - $bytesrecv)

if (StringLen($RecvBuffer) >= $lastpack) Then

Return 1

Else

Return 0

EndIf

EndIf

EndFunc ;==>RecvIt

Func RecvHeader($socket1, $Location1, ByRef $fname1, ByRef $fsize1, $packetsize1, ByRef $RecvBuffer)

Local $fname, $fsize

$RecvBuffer = $RecvBuffer & TCPRecv($socket1, $packetsize1)

If $RecvBuffer <> "" Then

$CheckedHeader = CheckHeader($RecvBuffer, $Location1, $fname, $fsize)

If $CheckedHeader = 1 Then

$fname1 = $fname

$fsize1 = $fsize

TCPSend($socket1, "HeaderOk!")

Return 1

Else

If $CheckedHeader = -1 Then TCPSend($socket1, "ErrorInFilename!")

If $CheckedHeader = -2 Then TCPSend($socket1, "FileTooBig!")

Return $CheckedHeader

EndIf

Else

;TCPSend($socket1,"Error_RecvHeader!")

;Disconnect($socket1)

Return 0

EndIf

EndFunc ;==>RecvHeader

Func CheckHeader($RecvString, $location, ByRef $Filename1, ByRef $FileSize1)

Local $RegExp1 = "Header:(.*?);([0-9]{1,255});HeaderEnd"

If StringRegExp($RecvString, $RegExp1) Then

$Header = StringRegExp($RecvString, $RegExp1, 1)

If UBound($Header) = 2 Then

If CheckLegalFilename($Header[0]) <> 1 Then

Return -1

Else

If CheckFreeSpace(Number($Header[1]), $location) Then

$FileSize1 = Number($Header[1])

$Filename1 = $Header[0]

Return 1

Else

Return -2

EndIf

EndIf

Else

Return 0

EndIf

Else

if ($RecvString = "VersionConflict!") Then

;msgbox(0,"",$RecvString)

Return -3

Else

Return 0

EndIf

EndIf

EndFunc ;==>CheckHeader

Func CheckLegalFilename($Name1)

Local $lengte = StringLen($Name1)

If $lengte > 255 Then Return 0

Local $RegExp1 = '[^;\\/:*?"<>|]{' & $lengte & '}'

Return StringRegExp($Name1, $RegExp1)

EndFunc ;==>CheckLegalFilename

Func CheckFreeSpace($Size, $location)

if ((DriveSpaceFree($location) * 1024) * 1024) > $Size Then

Return 1

Else

Return 0

EndIf

EndFunc ;==>CheckFreeSpace

Func Listen($ip1, $Port1)

Return TCPListen($ip1, $Port1, 0)

EndFunc ;==>Listen

Func Accept($socket1)

$returnval = TCPAccept($socket1)

If $returnval <> - 1 Then

Return $returnval

Else

Return -1

EndIf

EndFunc ;==>Accept

Func Disconnect($socket1)

TCPCloseSocket($socket1)

EndFunc ;==>Disconnect

Func Exitme()

DllClose($dll)

TCPShutdown()

Exit

EndFunc ;==>Exitme

;

Share this post


Link to post
Share on other sites

THX!

Great! But I did not expect it to be that... huge. Anyway, seems to be useful

for my nether plans... har har har.

Thanks alot, you realy made my day :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0