Jump to content
faustf

tcp transfer problem

Recommended Posts

faustf

hi guys  i  have create a script  for transfer a file

1 script

_SendFile(@ScriptDir & "\insert_prod.txt", (5 * 1024), "192.168.1.8")

Func _SendFile($File, $MaxLen,$iP)

    TCPStartup()
    Local $BytesRead = 0

    $Size = FileGetSize($File)
    If $Size = 0 Then Return SetError(1, 0, -1)

    ;get file name
    $Reg = StringRegExp($File, "(.)+\\((.)+)?", 3)

    ConsoleWrite($Reg & @CRLF)
    Select
        Case Not IsArray($Reg)
            $Name = $Reg
        Case UBound($Reg) < 2
            Return SetError(2, 0, -1)
        Case Else
            $Name = $Reg[1]
    EndSelect

    $Socket = TCPConnect($iP, 1312)
    If @error Then Return SetError(3, @error, -1)

    ;wait until something is sent
    Do
        $Receive = TCPRecv($Socket, 1000)
        If @error Then Return SetError(4, 0, -1)
        Sleep(10)
    Until $Receive <> ""

    If $Receive <> "Sending Data" Then Return SetError(5, $Receive, -1)

    ;Send Name and file size to receiver
    TCPSend($Socket, $Name & ":" & $Size)

    ;Wait for confirmation from receiver
    Do
        $Receive = TCPRecv($Socket, 1000)
        If @error Then SetError(6, 0, -1)
    Until $Receive <> ""

    If $Receive <> "Start Upload" Then SetError(7, $Receive, -1)

    ;open file to read in binary
    $FileHandle = FileOpen($File, 16)

    ;loop until the whole file is received
    While 1
        $Data = FileRead($FileHandle, $MaxLen)
        If @error Then ExitLoop
        $BytesRead += TCPSend($Socket, $Data)

    WEnd
    FileClose($FileHandle)
    TCPCloseSocket($Socket)

    Return SetError(0, 0, 1)
EndFunc   ;==>_SendFile

and reciver  script is

#include <GUIConstantsEx.au3>

#include <MsgBoxConstants.au3>

#include <Array.au3>



Global Const $WS_CAPTION = 0x00C00000

Global Const $WS_POPUP = 0x80000000

Global Const $WS_POPUPWINDOW = 0x80880000

Global Const $WS_EX_ACCEPTFILES = 0x00000010

Global Const $TCS_FIXEDWIDTH = 0x00000400

Global Const $PBS_SMOOTH = 1

Global Const $SS_CENTER = 1



; Assign Local variables the loopback IP Address and the Port.

Local $sIPAddress = @IPAddress1 ; This IP Address only works for testing on your own computer.

Local $iPort = IniRead(@ScriptDir & "\configMacroServer.ini", "Param", "port", "1312") ; Port used for the connection.

Global $Dir = @ScriptDir

Local $Destination = @ScriptDir

$MaxLen = 5 * 1024 * 1024 ; 5MB massimo scarico





While 1



    _ReceiveFile($Dir, $MaxLen) ;file limit of 5 megabytes

    Switch @error

        Case 0

            MsgBox(262208, "TCP Sender", "Download Complete")

        Case 1

            MsgBox(16, "WSA:" & @error, "Unable to hook port!")

        Case 2

            MsgBox(16, "WSA:" & @error, "Lost connection!")

        Case 3

            MsgBox(16, "WSA:" & @error, "Lost connection!")

    EndSwitch

    Sleep(15)

WEnd





Func _ReceiveFile($Destination, $MaxLen)

    TCPStartup()

    Local $Return = False, $Bytes = 0

    $Listen = TCPListen(@IPAddress1, 1312)

    If @error Then Return SetError(1, 0, -1)



    ToolTip("Listening" & @CRLF & @IPAddress1 & @CRLF & 1312, 0, 0)



    ;wait for sender to connect

    Do

        $Socket = TCPAccept($Listen)

    Until $Socket <> -1



    $Receive = TCPRecv($Socket, $MaxLen)

    If @error Then Return SetError(2, 0, -1)



    TCPSend($Socket, "Sending Data")



    ;get file data

    ;wait for file name and size to be sent

    Do

        $Receive = TCPRecv($Socket, 1000)

        If @error Then Return SetError(3, 0, -1)

        Sleep(5)

    Until $Receive <> ""

    $FileData = StringSplit($Receive, ":", 2)



    $Destination &= "\" & $FileData[0]



    $FileHandle = FileOpen($Destination, 16 + 2 + 8)



    ;initiate download

    TCPSend($Socket, "Start Upload")



    While 1

        $Data = TCPRecv($Socket, $MaxLen, 1) ;receive data in binary

        If @error Then ExitLoop



        $Bytes += BinaryLen($Data) ;count number of bytes sent



        FileWrite($FileHandle, $Data) ;recreate the file sent



    WEnd

    FileClose($FileHandle)

    TCPCloseSocket($Socket)

    TCPCloseSocket($Listen)

    ToolTip("")



    Return SetError(0, 0, 1)

EndFunc   ;==>_ReceiveFile

but not  work anyone can help me to find a problems???  thankz at all

 

Share this post


Link to post
Share on other sites
Jos
4 minutes ago, faustf said:

but not  work anyone can help me to find a problems???  thankz at all

because it is wrong? :idiot:

  • What have YOU done to debug?
  • Where did it go wrong?

ps: Assume this is related to the other topic you posted so why did you start a new topic? (feels like I have asked this question before to you)

Jos 


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
faustf

so the strange is not  return error   but  app is  blocked

 

and  is similar to old topic , but is not continue of them (i thinked )

:)

 

Share this post


Link to post
Share on other sites
faustf

so i insert some consolewrite for look where  stop the script , because in scite not  have  nothing error

i saw  the script  arrive at this point and remaning in  this loop , (in  sender )

;wait until something is sent
    Do
        $Receive = TCPRecv($Socket, 1000)
        If @error Then Return SetError(4, 0, -1)
        ConsoleWrite($Receive & @CRLF)
        Sleep(10)
    Until $Receive <> ""

in reciver i notice remaing  in this point loop

Do



        $Receive = TCPRecv($Socket, 1000)

        ConsoleWrite($Receive&@CRLF)

        If @error Then Return SetError(3, 0, -1)

        Sleep(5)

    Until $Receive <> ""

i dont know  why .

the  sender open tcpip (is ok )---> open a tcpconnection (is ok) ----> and go in the loop (wait a string , for start a transfer) (is ok)

the reciver open tcpstart (is ok ) ----> go in listen in ip address local on port correct (is ok) -----> and go in loop for  take acept connection

when a sender connect go out loop (is ok ) , at this point  send a string  "Sending Data"  (is ok ) -----> and go in loop for recive a data, from sender

but this "Sending Data" not arrive , i suppose or i dontknow   why

some idea???  thankz

Share this post


Link to post
Share on other sites
faustf

i  have do some modify , and  not work now  but i  dont see a problem ,

sender script

Local $BytesRead = 0

    $Size = FileGetSize($File)
    If $Size = 0 Then Return SetError(1, 0, -1)

    ;get file name
    $Reg = StringRegExp($File, "(.)+\\((.)+)?", 3)

    ConsoleWrite($Reg & @CRLF)
    Select
        Case Not IsArray($Reg)
            $Name = $Reg
        Case UBound($Reg) < 2
            Return SetError(2, 0, -1)
        Case Else
            $Name = $Reg[1]
    EndSelect
    TCPStartup()
    $Socket = TCPConnect($iP, 1312)
    ;If @error Then Return SetError(3, @error, -1)
    TCPSend($Socket, "Button Prod")
    ;wait until something is sent
    Do
        $Receive = TCPRecv($Socket, 1000)
        If @error Then Return SetError(4, 0, -1)
        ConsoleWrite($Receive & @CRLF)
        ConsoleWrite("Recive 1103" & @CRLF)
        Sleep(10)
    Until $Receive <> ""

    If $Receive <> "Sending Data" Then Return SetError(5, $Receive, -1)

    ;Send Name and file size to receiver
    TCPSend($Socket, $Name & ":" & $Size)
    If @error Then
        $iError = @error
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not send the data, Error code: " & $iError)
        Return False
    EndIf
    ;Wait for confirmation from receiver
    Do
        $Receive = TCPRecv($Socket, 1000)
        If @error Then SetError(6, 0, -1)
    Until $Receive <> ""

    If $Receive <> "Start Upload" Then SetError(7, $Receive, -1)

    ;open file to read in binary
    $FileHandle = FileOpen($File, 16)

    ;loop until the whole file is received
    While 1
        $Data = FileRead($FileHandle, $MaxLen)
        If @error Then ExitLoop
        $BytesRead += TCPSend($Socket, $Data)
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not send the data, Error code: " & $iError)
            Return False
        EndIf
    WEnd
    FileClose($FileHandle)
    TCPCloseSocket($Socket)

    Return SetError(0, 0, 1)

reciver script

#cs ----------------------------------------------------------------------------



    AutoIt Version: 3.3.14.2

    Author:         faustf



    Script Function:

    Template AutoIt script.

    Server lancia Macro

#ce ----------------------------------------------------------------------------



#include <GUIConstantsEx.au3>

#include <MsgBoxConstants.au3>

#include <Array.au3>



Global Const $WS_CAPTION = 0x00C00000

Global Const $WS_POPUP = 0x80000000

Global Const $WS_POPUPWINDOW = 0x80880000

Global Const $WS_EX_ACCEPTFILES = 0x00000010

Global Const $TCS_FIXEDWIDTH = 0x00000400

Global Const $PBS_SMOOTH = 1

Global Const $SS_CENTER = 1



; Assign Local variables the loopback IP Address and the Port.

Local $sIPAddress = @IPAddress1 ; This IP Address only works for testing on your own computer.

Local $iPort = IniRead(@ScriptDir & "\configMacroServer.ini", "Param", "port", "1312") ; Port used for the connection.

Global $Dir = @ScriptDir

Local $Destination = @ScriptDir

$MaxLen = 5 * 1024 * 1024 ; 5MB massimo scarico

Local $probe



While 1





    Local $Result = _ListenWaitServer(@IPAddress1, "1312")



    Sleep(15)

WEnd



Func _ListenWaitServer($ipaddress, $port)

    TCPStartup()

    $Listen = TCPListen($ipaddress, $port)

    If @error Then Return SetError(1, 0, -1)



    Do

        $Socket = TCPAccept($Listen)

        If @error Then

            $iError = @error

            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not accept the incoming connection, Error code: " & $iError)

            Return False

        EndIf



    Until $Socket <> -1



    TCPCloseSocket($Listen)

    Do

        $Receive = TCPRecv($Socket, 1000)

        ConsoleWrite($Receive & @CRLF)

        If @error Then Return SetError(3, 0, -1)

        Sleep(5)

    Until $Receive <> ""



    Select

        Case $Receive = "Button test"

            Local $SendReturn = TCPSend($Socket, "Connection ok")

            TCPCloseSocket($Socket)



        Case $Receive = "Button Prod"

            ToolTip("Listening" & @CRLF & @IPAddress1 & @CRLF & 1312, 0, 0)

            Local $SendReturn = TCPSend($Socket, "Sending Data")



            If @error Then

                $iError = @error

                MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not send the data, Error code: " & $iError)

                Return False

            EndIf



            ;get file data

            ;wait for file name and size to be sent



            Do



                $Receive = TCPRecv($Socket, 1000)

                ConsoleWrite($Receive & @CRLF)

                If @error Then Return SetError(3, 0, -1)

                Sleep(5)

            Until $Receive <> ""

            $FileData = StringSplit($Receive, ":", 2)



            $Destination &= "\" & $FileData[0]



            $FileHandle = FileOpen($Destination, 16 + 2 + 8)



            ;initiate download

            TCPSend($Socket, "Start Upload")





            Do

                $Data = TCPRecv($Socket, $MaxLen)

                ConsoleWrite($Data & @CRLF)

                If @error Then MsgBox(0,'Errore ricezione',@error)



            Until $Data <> ""





            FileWrite($FileHandle, $Data) ;recreate the file sent

            MsgBox(0,$Destination, $Data)



                    FileClose($FileHandle)

                    TCPCloseSocket($Socket)

                    ToolTip("")



            Return SetError(0, 0, 1)

    EndSelect



EndFunc   ;==>_ListenWaitServer

in practic  i saw  in console  the tcp  work fine send and recive  afile

but not  write file in folder why ??? o_O

thankz at all

 

Share this post


Link to post
Share on other sites
faustf

i insert this  in 

$FileHandle = FileOpen($Destination, 2 + 8)
If $FileHandle = -1 Then

                MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")

                Return False

            EndIf

and  i saw return me error impossible write temporary file , how  is possible happen this ??  , the path is a folder over my desktop

and  i set  2+8  in  file open

the script  should , create if not exist a file ?  right ??

thankz agian

Share this post


Link to post
Share on other sites
faustf

yes is valid  i resolve a problem with this  code

$FileHandle = FileOpen($Destination, 2)

remove  8   because   create a folder with name and extension of file txt

like this

c:\pippo.txt\pippo.txt  , the 1° poppio.txt is folder

o_O

but now after mod work correct all :D  thankz  Friends :)

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

×