Jump to content
Sign in to follow this  
Andreik

Print a file

Recommended Posts

Andreik

I have 3 computers and I want to create 2 scripts (server/client). I want that my client to send a file and server (from another computer) to receive the file and than print this file.

I tried with this functions written by GtaSpider.

;===============================================================================
;
; Function Name:   _FileSend
; Description:: Sending a File to an Server (See _FileReceive)
; Parameter(s):
;$sFile  : The File to send
;$IP                        : The target IP Adress
;$PORT [Optional]        : The Port to use (must be the same as by _FileRecevie!!)
;$iSplash [Optional]            : 1(defaul) = show SplashText
;                   0 = dont show SplashText
;$iWaitWhileConnected [Optional]    : 0 = If TCPConnect faild (@error) Return with error Code 2
;                   1(Default) = If TCPConnect faild put it in a while until connected
; Requirement(s):  -
; Return Value(s):  On Succes Return 1
;        On faild Return -1 and @error code 1-3:
;        @error = 1: File not exist
;        @error = 2: TCPConnect faild (only able if $iWaitWhileConnected <> 1)
;        @error = 3: Faild to Open File
;        @error = 4: A Connectionproblem while sending
; Author(s):       GtaSpider
;
;===============================================================================

Func _FileSend($sFile, $IP, $PORT = 4324, $iSplash = 1, $iWaitWhileConnected = 1)
    Local $iMainSocket = -1, $sBuff, $iFileOp, $sRecv

    If Not FileExists($sFile) Then Return SetError(1, 0, -1)
    If $iSplash Then $iSplash = SplashTextOn('', 'Try To Connect...', 200, 20, -1, -1, 1)
    TCPStartup()

    If Number($iWaitWhileConnected) = 1 Then
        While $iMainSocket = -1
            $iMainSocket = TCPConnect($IP, $PORT)
        WEnd
    Else
        $iMainSocket = TCPConnect($IP, $PORT)
        If @error Then Return SetError(2, 0, -1)
    EndIf
    If $iSplash Then ControlSetText($iSplash, '', 'Static1', 'Read File...')
    $iFileOp = FileOpen($sFile, 16)
    If @error Then Return SetError(3, 0, -1)
    $sBuff = Binary(StringTrimLeft($sFile, StringInStr($sFile, "\", -1, -1)) & ",") & FileRead($iFileOp)
    FileClose($iFileOp)
    If $iSplash Then ControlSetText($iSplash, '', 'Static1', 'Sending File...')
    While BinaryLen($sBuff)
        $iSendReturn = TCPSend($iMainSocket, $sBuff)
        If @error Then Return SetError(4, 0, -1)
        TrayTip('', $iSendReturn, 1)
        $sBuff = BinaryMid($sBuff, $iSendReturn + 1, BinaryLen($sBuff) - $iSendReturn)
    WEnd
    If $iSplash Then ControlSetText($iSplash, '', 'Static1', 'Succesfull!')
    TCPCloseSocket($iMainSocket)
    TCPShutdown()
    Sleep(1000)
    SplashOff()
    Return 1
EndFunc

;===============================================================================
;
; Function Name:   _FileReceive
; Description:: Receives a File from an Client (See _FileSend)
; Parameter(s):   
;$sFileName [Optional]    : The Filename to save (if default or "" then using the original File Name)
;$IP [Optional] : The IP where the Server run, Default = @IPAdress1
;$PORT [Optional]        : The Port to use (must be the same as by _FileSend!!)
;$iSplash [Optional]            : 1(defaul) = show SplashText
; Requirement(s):  -
; Return Value(s):  On Succes Return 1
;        On faild Return -1 and @error code 1-3:
;        @error = 1: Error creating Listening socket on IP
;        @error = 2: Faild to open file
;        @error = 3: Faild to write file
; Author(s):       GtaSpider
;
;===============================================================================
 
Func _FileReceive($sFileName = '', $IP = @IPAddress1, $PORT = 4324,$iSplash=1)
    Local $iMainSocket, $iAccSocket = -1, $sBuff, $sRecv = "", $i = 0, $iFirstWhile = True
    TCPStartup()
 
    $iMainSocket = TCPListen($IP, $PORT)
    If @error Then Return SetError(1,0,-1)
    If $iSplash Then $iSplash = SplashTextOn('','Wait For Incoming...',200,20,-1,-1,1)
    While $iAccSocket = -1
        $iAccSocket = TCPAccept($iMainSocket)
        Sleep(50)
    WEnd
    $sBuff = Binary ($sBuff)
   
    If $iSplash Then ControlSetText($iSplash,'','Static1','Incoming...')
   
    While $sRecv = ""
        $sRecv = TCPRecv($iAccSocket, 2048, 1)
        $sRecv = BinaryToString ($sRecv)
    WEnd
    If $iSplash Then ControlSetText($iSplash,'','Static1','Receive File...')
    While $sRecv <> ""
        If StringInStr($sRecv, ',') And $iFirstWhile Then
            $sTmp = StringLeft($sRecv, StringInStr($sRecv, ",") - 1)
            $sRecv = StringTrimLeft($sRecv, StringLen($sTmp) + 1)
            If StringLen($sFileName) < 1 Then $sFileName = $sTmp
            $iFirstWhile = False
        EndIf
        $sBuff &= $sRecv
        $sRecv = BinaryToString (TCPRecv($iAccSocket, 2048, 1))
        If @error Then ExitLoop
    WEnd
    If $iSplash Then ControlSetText($iSplash,'','Static1','Write To File...')
    If FileExists($sFileName) Then
        $sTmp = StringSplit($sFileName, ".")
        If $sTmp[0] < 2 Then
            While 1
                $i += 1
                If Not FileExists($sFileName & "(" & $i & ")") Then
                    $sFileName = $sFileName & "(" & $i & ")"
                    ExitLoop
                EndIf
            WEnd
        Else
            While 1
                $i += 1
                If Not FileExists($sTmp[1] & "(" & $i & ")" & $sTmp[2]) Then
                    $sFileName = $sTmp[1] & "(" & $i & ")." & $sTmp[2]
                    ExitLoop
                EndIf
            WEnd
        EndIf
    EndIf
    $iFileOp = FileOpen($sFileName, 16 + 2)
    If @error Then Return SetError(2,0,-1)
    FileWrite($iFileOp, $sBuff)
    If @error Then Return SetError(3,0,-1)
    FileClose($iFileOp)
    If $iSplash Then ControlSetText($iSplash,'','Static1','Succesfull!')
    TCPCloseSocket($iAccSocket)
    TCPShutdown()
    Sleep(1000)
    Return 1
EndFunc;==>_FileReceive

But I don't know extension of file (_FileRecv) so I can't use _FileRecv in this form.

Any idea?

Edited by Andreik

When the words fail... music speaks

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.