Jump to content

How do I send a file over tcp (single bytes)


Go to solution Solved by FireFox,

Recommended Posts

Posted

I have an application that already communicates with a hardware device. I would like to transfer small files, and managed to read files. But I need to find out how to write (send) files.

The procedure will be as follows:

  • Send a command to store file on port 3000 (no problem)
  • Listen for response "send file now"
  • Connect on port 3100
  • Send raw bytes of file (preferably in adjustable steps from 1..n bytes each time, as I am unsure of input buffer size)
  • When done, listen to port 3000 for "OK, received"
  • Close port 3100

 

My problem essentially is point 4: how to use tcpsend($transferport, $string_of_bytes), where length of $string_of_bytes is adjustable.

We have a code written in Delphi (this is the application I wish to improve on), this one uses

TransferSocket.Socket.Sendstream(file, 0);

 

However I would also like to have a progressbar, so should probably use single byte sending.

Thanks for any input.

  Reveal hidden contents

I am just a hobby programmer, and nothing great to publish right now.

Posted

FireFox,

so something like this (untested, since I have work-stuff to do first)

    ; Assign a Local variable the size of the file previously chosen.
    Local $iFileSize = FileGetSize($sFilePath)

    ; Assign a Local variable the handle of the file opened in binary mode.
    Local $hFile = FileOpen($sFilePath, $FO_BINARY)

    ; Assign a Local variable the offset of the file being read.
    Local $iOffset = 0

    ; Assign a Local variable the number representing 4 KiB.
    ; ** or some other value which will be out chunck size **
    Local Const $i4KiB = 4096

    ; Note: The file is send by parts of 4 KiB.

    ; Send the binary data of the file to the server.
    Do
        ; Set the file position to the current offset.
        FileSetPos($hFile, $iOffset, $FILE_BEGIN)

        ; The file is read from the position set to 4 KiB and directly wrapped into the TCPSend function.
        TCPSend($iSocket, FileRead($hFile, $i4KiB))

        ; If an error occurred display the error code and return False.
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not send the data, Error code: " & $iError)

            ; Close the socket.
            TCPCloseSocket($iSocket)
            Return False
        EndIf

        ; Increment the offset of 4 KiB to send the next 4 KiB data.
        $iOffset += $i4KiB
    Until $iOffset >= $iFileSize

    ; Close the file handle.
    FileClose($hFile)

 

If that's all, I think I have it pretty much nailed...

  Reveal hidden contents

I am just a hobby programmer, and nothing great to publish right now.

Posted

FireFox worked really hard on improving the TCP/UDP help file functions.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
  • Recently Browsing   0 members

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