Jump to content

File Transfer


Recommended Posts

Not sure what's going on here, but I'm using one port for all the data between my client and server. The server is doing a stringsplit($data, '|') to figure out what to do with the data ie: " ¦COMPINFO|blah|blah2|blah3 ¦"

I am using this character to signify the beginning/end of a command '¦'.

Right now I am trying to send a jpg that is around 25kb, but I'm receiving around 48kb.

Here is some example code from the client and server.

I have greatly simplified what I had in order to try to get SOMETHING to work.

CLIENT

Func _sendfile($file)
    $FileOpen = FileOpen($file,16)  
    $FileSize = FileGetSize($file)
    $BytesSent = 0
    sleep(10)
    $ReadFile = FileRead($FileOpen)
    $BytesSent += TCPSend($socket, $ReadFile)
    If @error Then
        FileClose($FileOpen)    
        MsgBox(0,'error','connection failed')
    EndIf
;MsgBox(0,'total',$BytesSent)
    FileClose($FileOpen)
EndFunc

SERVER

;NOTE: $filename has already been sent by the client
Global $file = FileOpen(StringTrimLeft($filename,StringInStr($filename,'\',0,-1)), 1+16)
FileWrite($file, $data[$i])
FileClose($file)

I've spent at least 15 hours on this one problem. Any help would be greatly appreciated.

Link to comment
Share on other sites

Not sure what's going on here, but I'm using one port for all the data between my client and server. The server is doing a stringsplit($data, '|') to figure out what to do with the data ie: " ¦COMPINFO|blah|blah2|blah3 ¦"

I am using this character to signify the beginning/end of a command '¦'.

Right now I am trying to send a jpg that is around 25kb, but I'm receiving around 48kb.

Here is some example code from the client and server.

I have greatly simplified what I had in order to try to get SOMETHING to work.

CLIENT

Func _sendfile($file)
     $FileOpen = FileOpen($file,16)  
     $FileSize = FileGetSize($file)
     $BytesSent = 0
     sleep(10)
     $ReadFile = FileRead($FileOpen)
     $BytesSent += TCPSend($socket, $ReadFile)
     If @error Then
         FileClose($FileOpen)   
         MsgBox(0,'error','connection failed')
     EndIf
;MsgBox(0,'total',$BytesSent)
     FileClose($FileOpen)
 EndFunc

SERVER

;NOTE: $filename has already been sent by the client
 Global $file = FileOpen(StringTrimLeft($filename,StringInStr($filename,'\',0,-1)), 1+16)
 FileWrite($file, $data[$i])
 FileClose($file)

I've spent at least 15 hours on this one problem. Any help would be greatly appreciated.

You are appending rather than overwriting your file.

Global $file = FileOpen(StringTrimLeft($filename,StringInStr($filename,'\',0,-1)), 1+16);<--------should be 16+2
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I tried changing it to 16+2, but then I don't receive the whole file.

Also, I want to eventually use a loop to send/receive, because otherwise I'll have to put the entire file into the memory and send it all at once, which may not be possible for large files.

Link to comment
Share on other sites

I tried changing it to 16+2, but then I don't receive the whole file.

Also, I want to eventually use a loop to send/receive, because otherwise I'll have to put the entire file into the memory and send it all at once, which may not be possible for large files.

I tried sending a file like this

;--------------------------
    Local $ConnectedSocket, $szData
   ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = "192.168.1.104";<----I set the IP of the server here
    ConsoleWrite($szIPADDRESS & @CRLF)
   ;Exit
    Local $nPORT = 33891

   ; Start The TCP Services
   ;==============================================
    TCPStartup()

   ; Initialize a variable to represent a connection
   ;==============================================
    $socket = -1

   ;Attempt to connect to SERVER at its IP and PORT 33891
   ;=======================================================
    $socket = TCPConnect($szIPADDRESS, $nPORT)
   _sendfile("test.jpg");<------------- set to a file I could send as a test


Func _sendfile($file)
    $FileOpen = FileOpen($file,16)
    $FileSize = FileGetSize($file)
    $BytesSent = 0
    sleep(10)
    $ReadFile = FileRead($FileOpen)
    $start = 1
    $len = binarylen($readfile)
    ConsoleWrite("length of binary dayta = " & $len & @CRLF)
    $part = 1024
    while 1
        if $len - $start >= 1024 then
         $part = 1024
     Else
         $part = $len - $start
         Endif


    $BytesSent += TCPSend($socket, binarymid($ReadFile,$start,$part))
    ConsoleWrite("sent = " & $BytesSent & ', ' & $part & @CRLF)
     If @error Then
        FileClose($FileOpen)
        MsgBox(0,'error','connection failed')
    EndIf
    $start += $part
    if $start >= $len then exitloop
    sleep(2000)
    WEnd

MsgBox(0,'total',$BytesSent)
    FileClose($FileOpen)
EndFunc

and receiving it like this

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()

    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv, $f, $r2 = Binary("")

   ; Start The TCP Services
   ;==============================================
    TCPStartup()

   ; Create a Listening "SOCKET".
   ;   Using your IP Address and Port 33891.
   ;==============================================
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

   ; If the Socket creation fails, exit.
    If $MainSocket = -1 Then Exit


   ; Create a GUI for messages
   ;==============================================
    $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
    GUISetState()


   ; Initialize a variable to represent a connection
   ;==============================================
    $ConnectedSocket = -1


   ;Wait for and Accept a connection
   ;==============================================
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1


   ; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)

   ; GUI Message Loop
   ;==============================================
    $f = FileOpen("testtcpin01.jpg", 16 + 2)
    While 1
        $msg = GUIGetMsg()

       ; GUI Closed
       ;--------------------
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop

       ; Try to receive (up to) 2048 bytes
       ;----------------------------------------------------------------
        $recv = TCPRecv($ConnectedSocket, 2048, 1)

       ; If the receive failed with @error then the socket has disconnected
       ;----------------------------------------------------------------
        If @error Then ExitLoop

       ; Update the edit control with what we have received
       ;----------------------------------------------------------------

        If $recv <> "" Then

            $r2 &= $recv
            ConsoleWrite("rcv len = " & BinaryLen($r2) & @CRLF)
        EndIf


    WEnd
    If $r2 <> "" Then
        FileWrite($f, $r2)
        FileClose($f)
    EndIf
    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    TCPShutdown()
EndFunc  ;==>Example

and I could send a jpg successfully.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

There are tons of these. Here is one I made a while ago.

Func sendFile($file, $delete = 0)
    $FileOpen = FileOpen($file, 16)     ;open in binary
    $FileSize = FileGetSize($file)
    $BytesSent = 0
    sendPacket(String($FileSize))               ;send hte file size initially so other program and do progress bars and such
    sleep(100)
   
    While $BytesSent < $FileSize                    ;control to stop the loop and stop sending
        $ReadFile = FileRead($FileOpen, 4096)
        $BytesSent += TCPSend($socket, $ReadFile)   ;adds bytes to know when to stop the loop
        If @error Then
            FileClose($FileOpen)
            If $delete = 1 Then FileDelete($file)
            connFail()
        EndIf
        sleep(20) ;allows other computer time to write to file can be adjusted accordingly
    WEnd
    sleep(20)
    sendPacket("Transfer Complete")                 ;lets other computer know we are done sending
    FileClose($FileOpen)
    If $delete = 1 Then FileDelete($file)
    $timeout = TimerInit() ;resets global time
EndFunc

Func getFile($file, $delete = 0)
    Local $time = 0
    $file = FileOpen($file, 18)     ;open in binary
   
    $time = TimerInit()       ;starst a timeout timer  so we aren't trying to send forever
    Do
        $recv = TCPRecv($socket, 17520)
        $dif = TimerDiff($time)
        If $dif >= 60000 Then               ;one minute
            If $delete = 1 Then     ;deletes the file if connection fails
                FileClose($file)
                FileDelete($file)
            EndIf
        connFail() ;timeout UDF to kill the application and close GUIS
        EndIf
        sleep(1)
        If $recv <> "Transfer Complete" Then FileWrite($file, $recv)
    Until $recv = "Transfer Complete"
    FileClose($file)
    $timeout = TimerInit() ;resets global time
EndFunc
Link to comment
Share on other sites

I've been toying with both of your responses for a long time: the problem is that my server is far more complicated.

I use a server similar in style to the one that Martin posted, but I use a StringSplit on the data, then I loop through this array and use a switch to figure out what to do with it.

Here is basically the design:

Func translate($data, $sender)   ;data is the raw data from tcprecv, $sender is from the array of sockets.  sockets[$sender]
    $datas = StringSplit($data, '¦')
    For $o = 1 To $datas[0] Step 1
        Select
            Case StringStripWS($datas[$o], 8) = ''
            Case $datas[$o] = '.'
                $socket[$sender][1] = TimerInit()
            Case $datas[$o] = '~EOF~'
                FileClose($file)
                MsgBox(0,'EOF','found ' & $file)
            Case $file > 0  ;RECEIVING!
            ;$datas[$o] = StringToBinary($datas[$o])
                FileWrite($filename,$datas[$o])

            Case StringLeft($datas[$o], 9) = 'filename|' 
                $temp = StringSplit($datas[$o],'|')
                $filename = $temp[2]
                $filesize = $temp[3]
                Global $file = FileOpen(StringTrimLeft($filename,StringInStr($filename,'\',0,-1)), 1+8+16)
                MsgBox(0,'file=',$file)
                        Case Else
                                ConsoleWrite($datas[$o])
        EndSelect
    Next
EndFunc  ;==>translate

I've changed the style and simplicity of this function and the sending function repeatedly, but every time the file I receive comes in at least twice the size it should be.

Link to comment
Share on other sites

I've been toying with both of your responses for a long time: the problem is that my server is far more complicated.

I use a server similar in style to the one that Martin posted, but I use a StringSplit on the data, then I loop through this array and use a switch to figure out what to do with it.

Here is basically the design:

Func translate($data, $sender) ;data is the raw data from tcprecv, $sender is from the array of sockets.  sockets[$sender]
      $datas = StringSplit($data, '¦')
      For $o = 1 To $datas[0] Step 1
          Select
              Case StringStripWS($datas[$o], 8) = ''
              Case $datas[$o] = '.'
                  $socket[$sender][1] = TimerInit()
              Case $datas[$o] = '~EOF~'
                  FileClose($file)
                  MsgBox(0,'EOF','found ' & $file)
              Case $file > 0;RECEIVING!
            ;$datas[$o] = StringToBinary($datas[$o])
                  FileWrite($filename,$datas[$o])
  
              Case StringLeft($datas[$o], 9) = 'filename|' 
                  $temp = StringSplit($datas[$o],'|')
                  $filename = $temp[2]
                  $filesize = $temp[3]
                  Global $file = FileOpen(StringTrimLeft($filename,StringInStr($filename,'\',0,-1)), 1+8+16)
                  MsgBox(0,'file=',$file)
                          Case Else
                                  ConsoleWrite($datas[$o])
          EndSelect
      Next
  EndFunc;==>translate

I've changed the style and simplicity of this function and the sending function repeatedly, but every time the file I receive comes in at least twice the size it should be.

How do you create the data which is sent? It would be worth converting the data back the way you showed but on th edata to be transmitted to test that the reverse process works as you expect without the TCPSend/recv adding to the confusion.

I am not sure how it should work because if you are converting binary data to a string and then adding strings together separated by '|', how do you know that the binary to string conversion doesn't include the sparator character?

If you are using BinaryToString to create the data sent then why have you commented out the line "$datas[$o] = StringToBinary($datas[$o])".

It might be better to keep all the data sent an binary. I would try something like

$SSend = dllstructcreate("char[" & $MAXHEADERLEN & "] header;int datalen; byte[" & $MAXDATALEN & "] data")

then set the header and the data as string and binary. The header could be ''.

Set the data length - DllStructSetData($SSend,"datalen",BinaryLen($data))

$SSendB = dllstructcreate("byte[" & DllStructGetSize($SSEND) & " alldata]",DllstructGetPtr($SSend));EDIT 1

$toSend = DllStructGetData($SSendB,"alldata");send this which is pure binary

At the receiving end create the same structs, plonk the binary data in the binary struct, read the header and binary length from the other struct and then you won't have any conversion problems and I think it will be easier. But of course it's easy for me to say that :D

EDIT1: added ptr to DllStructCreate which I missed out.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I commented out that line because I've tried it and it didn't work. I tried many variations, including a simple

stringtobinary($data)
then converting it back with
binarytostring($data)
, but that didn't give me anything better. I'll try messing with the lines you gave me, but I don't fully understand dll structs.

This is what I have so far

Func _sendfile($file)
    $file = FileOpen($file,16)
    $data = FileRead($file)
    $SSend = dllstructcreate("char[" & $MAXHEADERLEN & "] header;int datalen; byte[" & $MAXDATALEN & "] data")
    DllStructSetData($SSend,"datalen",BinaryLen($data))
    $SSendB = dllstructcreate("byte[" & DllStructGetSize($SSEND) & " alldata]")
    $toSend = DllStructGetData($SSendB,"alldata");send this which is pure binary
    TCPSend($socket,$toSend)
    If @error Then _send("FILE-ERROR")
    sleep(1000)
    _send('~EOF~')
EndFunc

What should I use for $MAXHEADERLEN and $MAXDATALEN?

Link to comment
Share on other sites

I commented out that line because I've tried it and it didn't work. I tried many variations, including a simple

stringtobinary($data)
then converting it back with
binarytostring($data)
, but that didn't give me anything better. I'll try messing with the lines you gave me, but I don't fully understand dll structs.

This is what I have so far

Func _sendfile($file)
     $file = FileOpen($file,16)
     $data = FileRead($file)
     $SSend = dllstructcreate("char[" & $MAXHEADERLEN & "] header;int datalen; byte[" & $MAXDATALEN & "] data")
     DllStructSetData($SSend,"datalen",BinaryLen($data))
     $SSendB = dllstructcreate("byte[" & DllStructGetSize($SSEND) & " alldata]")
     $toSend = DllStructGetData($SSendB,"alldata");send this which is pure binary
     TCPSend($socket,$toSend)
     If @error Then _send("FILE-ERROR")
     sleep(1000)
     _send('~EOF~')
 EndFunc

What should I use for $MAXHEADERLEN and $MAXDATALEN?

I think it needs to be more like this

Global Const $MAXHEADERLEN = 100;or however many character you think you might need
Global Const $MAXDATALEN = 4096;or whatever you decide the largest size of data will be
Func _sendfile($file)
   ;create struct for header, data size and data
    $SSend = DllStructCreate("char[" & $MAXHEADERLEN & "] header;int datalen; byte[" & $MAXDATALEN & "] data")
   ;create struct in same position of memory which will be just read as the bytes of the whole of $SSend
    $SSendB = DllStructCreate("byte[" & DllStructGetSize($SSend) & "] alldata]", DllStructGetPtr($SSend))

   ;open the file
    $hfile = FileOpen($file, 16)
    $filesize = FileGetSize($hfile)
   ;read the data
    $data = FileRead($hfile, $MAXDATALEN)
    DllStructSetData($SSend, "header", "filename|" & $file & "|" & $filesize);whatever you want the initial header to be
    DllStructSetData($SSend, "datalen", BinaryLen($data))
    DllStructSetData($SSend, "data", $data)
    $TotSent = 0
    While $TotSent < $filesize
        $toSend = DllStructGetData($SSendB, "alldata");send this which is pure binary
        TCPSend($socket, $toSend)
        If @error Then _send("FILE-ERROR")
        $TotSent += BinaryLen($data)
        $data = FileRead($hfile, $MAXDATALEN)
        DllStructSetData($SSend, "header", "more");something to say we are sending more of the file
        DllStructSetData($SSend, "datalen", BinaryLen($data))
        DllStructSetData($SSend, "data", $data)
        Sleep(1000);probably not needed
    WEnd

    FileClose($hfile)
    DllStructSetData($SSend, "header", "~EOF~");something to say we have finished
    DllStructSetData($SSend, "datalen", 0)
    $toSend = DllStructGetData($SSendB, "alldata")
    TCPSend($socket, $toSend)
    _send('~EOF~')
EndFunc  ;==>_sendfile

Then you need the corresponding code for receiving. No time now but I'll look at it again tonight.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Here is something I have tested on sending jpgs of around 5Mbytes. It has a header with each transmission, but needs to be adjusted as needed to your requirements.

The server which receives the file and must be started first

#include <GUIConstantsEx.au3>

;these next constants must be the same in both files- better to have an include with them in to ensure this
Global Const $MAXHEADERLEN = 100;or however many characters you think you might need
Global Const $MAXDATALEN = 5000;or whatever you decide the largest size of data will be but see note at end of this post

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!


Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891
Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
Local $msg, $recv, $f, $r2 = Binary("")

; Start The TCP Services
;==============================================
TCPStartup()

; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)

; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit


; Create a GUI for messages
;==============================================
$GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
$edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
GUISetState()


; Initialize a variable to represent a connection
;==============================================
$ConnectedSocket = -1


;create struct for header, data size and data
$SRcv = DllStructCreate("char header[" & $MAXHEADERLEN & "];int datalen;byte data[" & $MAXDATALEN & "]")
ConsoleWrite("str 1 size = " & Dllstructgetsize($srcv) & @CRLF)
;create struct in same position of memory which will be just read as the bytes of the whole of $SSend
$RECVSIZE = DllStructGetSize($SRcv)
$SRcvB = DllStructCreate("byte alldata[" & $RECVSIZE & "]", DllStructGetPtr($SRcv))
ConsoleWrite("str 1 size = " & Dllstructgetsize($srcvb) & @CRLF)



;Wait for and Accept a connection
;==============================================
Do
    $ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1



; GUI Message Loop
;==============================================
;$f = FileOpen("testtcpin01.jpg", 16 + 2)
While 1
    $msg = GUIGetMsg()

   ; GUI Closed
   ;--------------------
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

   ; Try to receive (up to) 2048 bytes
   ;----------------------------------------------------------------
    $recv = TCPRecv($ConnectedSocket, $RECVSIZE, 1)

   ; If the receive failed with @error then the socket has disconnected
   ;----------------------------------------------------------------
    If @error Then ExitLoop
    if $recv <> "" Then
    ConsoleWrite("bl = " & BinaryLen($recv) & @CRLF)
    DllStructSetData($SRcvB, "alldata", $recv)

    $header = DllStructGetData($SRcv, "header")
    Select
        Case StringInStr($header, "filename") = 1
            ConsoleWrite("filename" & @CRLF)
            $hf = fileopen("test01.jpg",16+2);<-----I didn't use the file name I just tested with a fixed name
            filewrite($hf,binarymid(dllstructgetdata($srcv,"data"),1,dllstructgetdata($srcv,"datalen")))
        Case StringInStr($header, "more") = 1
            ConsoleWrite("more" & @CRLF)
            filewrite($hf,binarymid(dllstructgetdata($srcv,"data"),1,dllstructgetdata($srcv,"datalen")))
        Case StringInStr($header, "~EOF~") = 1
            ConsoleWrite("EOF" & @CRLF)
            fileclose($hf)
        Case Else
            ConsoleWrite("error in received data, header =" & $header & @CRLF)
            ConsoleWrite("Blen = " & BinaryLen($recv) & @CRLF)
    EndSelect

EndIf

WEnd

TCPShutdown()

The sender

Global Const $MAXHEADERLEN = 100;or however many character you think you might need
Global Const $MAXDATALEN = 5000;or whatever you decide the largest size of data will be

;--------------------------
    Local $ConnectedSocket, $szData
    Local $szIPADDRESS = "192.168.1.104";<----I set the IP of the server here

    Local $nPORT = 33891

  ; Start The TCP Services
  ;==============================================
    TCPStartup()

  ; Initialize a variable to represent a connection
  ;==============================================
    $socket = -1

  ;Attempt to connect to SERVER at its IP and PORT 33891
  ;=======================================================
    $socket = TCPConnect($szIPADDRESS, $nPORT)
    $fs = FileOpenDialog("select the file to send","\","Pics(*.jpg)")
   _sendfile($fs);


Func _sendfile($file)
   ;create struct for header, data size and data
    $SSend = DllStructCreate("char header[" & $MAXHEADERLEN & "];int datalen;byte data[" & $MAXDATALEN & "]")
   ;create struct in same position of memory which will be just read as the bytes of the whole of $SSend
    $SSendB = DllStructCreate("byte alldata[" & DllStructGetSize($SSend) & "]", DllStructGetPtr($SSend))
 
   ;open the file
    $hfile = FileOpen($file, 16)
    $filesize = FileGetSize($file)
  ; ConsoleWrite($filesize & @CRLF)

   ;read the data
    $data = FileRead($hfile, $MAXDATALEN)
    DllStructSetData($SSend, "header", "filename|" & $file & "|" & $filesize)
    DllStructSetData($SSend, "datalen", BinaryLen($data))
    DllStructSetData($SSend, "data", $data)
    $TotSent = 0
    While $TotSent < $filesize;just a way to decide when to stop
        $toSend = DllStructGetData($SSendB, "alldata");send this which is pure binary
        ConsoleWrite("BL sent = " & binarylen($tosend) & @CRLF)
        TCPSend($socket, $toSend)
      ; If @error Then _send("FILE-ERROR")
        $TotSent += BinaryLen($data)
        ConsoleWrite("tot sent = " & $TotSent & @CRLF)
        $data = FileRead($hfile, $MAXDATALEN)
        DllStructSetData($SSend, "header", "more");something to say we are sending more of the file
        DllStructSetData($SSend, "datalen", BinaryLen($data))
        DllStructSetData($SSend, "data", $data)
      ; Sleep(1000)
    WEnd

    FileClose($hfile)
    DllStructSetData($SSend, "header", "~EOF~");something to say we have finished
    DllStructSetData($SSend, "datalen", 0)
    $toSend = DllStructGetData($SSendB, "alldata")
    TCPSend($socket, $toSend)
   ;_send('~EOF~')
EndFunc  ;==>_sendfile

I tried increasing the value of $MAXDATALEN but 10000 and above caused errors. I didn't try between 5000 and 10000, but any value I tried below 5000 worked fine.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Hmmm well I've been working with your suggestions and my own code for a LONG time and I think I've got it working...

sort of.

But the problem is that it SHOULDN'T work.

While $TotSent < $filesize
        $data = FileRead($hfile, $MAXDATALEN)
        _send($data)
        $TotSent += StringLen($data)/2
        Sleep(50)
    WEnd

It was only sending half of the jpg, so I added the "/2" to the while function inside of the sender.

Why is this working and what do I need to change to make it proper? I'm sure this rough code is going to make problems.

Link to comment
Share on other sites

Hmmm well I've been working with your suggestions and my own code for a LONG time and I think I've got it working...

sort of.

But the problem is that it SHOULDN'T work.

While $TotSent < $filesize
         $data = FileRead($hfile, $MAXDATALEN)
         _send($data)
         $TotSent += StringLen($data)/2
         Sleep(50)
     WEnd

It was only sending half of the jpg, so I added the "/2" to the while function inside of the sender.

Why is this working and what do I need to change to make it proper? I'm sure this rough code is going to make problems.

Does the file you are sending contain Unicode characters?
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

It's a jpg, so I think it contains unicode characters.

The code I am not using seems to be working, but occasionally it 'smears' or distorts the jpg... so I know something isn't right.

Can you post a small example of the code that causes a problem?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • 3 weeks later...

It happens when I read and tcp transfer the data to another script.

The first script gets the data with

$data = FileRead($file)

and sends it to the other script.

the second script has an open file handle and writes it with

FileWrite($file,$data)

I've tried throwing in BinaryToString(s) and StringToBinary(s)...

Any ideas?

Link to comment
Share on other sites

Problem solved.

I kept changing things and finally something worked.

On the receiving end, I have a Switch that checks the first few characters of the data for commands.

If it reaches the Else, it assumes it is the file data,

This is what I have inside the Else

$filedata &= $datas[$o]
                if StringLen(BinaryToString($filedata)) >= $filesize then; total file size received
                    $filehandle = FileOpen(@TempDir & '\virulence\' & $filename,18)
                    FileWrite($filehandle,$filedata)
                    FileClose($filehandle)
                EndIf

I hope it helps someone!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...