Jump to content

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


Myicq
 Share

Go to solution Solved by FireFox,

Recommended Posts

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.

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

Link to comment
Share on other sites

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...

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

Link to comment
Share on other sites

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

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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...