Jump to content

Best way to receive TCP data on a port ?


Myicq
 Share

Recommended Posts

I am a bit stuck here.. I have a device I am developing a download tool for.

The device works / sends like this:

Connection is by TCP/IP, in a client/repsonse fashion (issue command, get result etc etc)

Connection to port 3000 for commands. This is for remote control in general

Issue command for file download

Response with OK and file size

Connection to port 3100 for data.

Data is sent in raw binary form (hex codes directly)

Once all is sent on 3100, OK is sent on port 3000.

File size received should be same now as initially stated.

My problem is: often it's not.

The "official application" seems to work OK (this is written in Delphi). I must do something wrong.

This is what I have (in main routine).

_d("text") is for logging debug data

$s and $s1 are sockets.

Func dl($outfilename, $outfilesize)
    ; ConsoleWrite("<<< Downloading " & $outfilename & "  " & $outfilesize & @CRLF )
    _d(">>> In actual download function <<<")
    _d("Connecting port 3100")
    $s2 = TCPConnect($g_IP, "3100")
    local $r=''
    if $s2<>-1 then
        _d("There is a second connection $s2")
        ; consolewrite ("CONNECTED TO FILE PORT "& @CRLF )
        ; consolewrite ("OUTFILE NAME IS " & $outfilename & @CRLF)
        _d("Need to receive " & $outfilesize)

        ; set progressbar for file to nothing
        guictrlsetdata($Progress1, 0)
        AutoItSetOption("TCPTimeout",1)

        $doing = guictrlread($Label4)

        while binarylen($msg) < $outfilesize
            $r = TCPRecv($s2, 1024,1 )
            $howfar = round((binarylen($msg) / $outfilesize)*100, 0)
            ; _d(StringLen($r) & " bytes recd. = " & $howfar & " pct" )

            ; update progress bar
            guictrlsetdata($Progress1, $howfar)
            $r = binarytostring($r)
            ; consolewrite($r)
            $msg = $msg & $r
            _logit($doing & " (" & $howfar & " %)")

            ; try a small pause here..
            ;sleep(120)
            $a = TCPRecv($s, 40)
            if StringRegExp($a, "done OK#") Then
                ; we are done
                _logit("Transfer done OK received")
                if binarylen($msg) < $outfilesize then
                    ; we did not receive all...
                    msgbox(0,"ERROR", "We did not receive it all before getting OK!"& @CRLF & _
                            "Got => " & binarylen($msg) & @CRLF  & _
                            "Need =>  " & $outfilesize)
                    tcpsend($s, "CMD:D#")
                    TCPCloseSocket($s)
                    TCPCloseSocket($s2)
                    Exit
                EndIf
            Elseif StringRegExp($a, "timeout#") then
                MsgBox(0,"ERROR", "FAIL: " & $a)
                tcpsend($s, "CMD:D#")
                TCPCloseSocket($s)
                TCPCloseSocket($s2)
                Exit
            EndIf

        wend
        ; set progressbar for file to nothing
        guictrlsetdata($Progress1, 0)

        _d("Ready to write " & $outfilename)
        $outfil = FileOpen($outfilename, 16 + 2 + 8 )        ; create dir structure if needed...
        FileWrite($outfil, $msg)
        FileClose($outfil)
        $msg=''
    Else
        MsgBox(0,"","ERROR no $socket")
    EndIf
    _d("Closing socket $s2")
    TCPCloseSocket($s2)
EndFunc   ;==>dl

Any pointers on how best to receive raw data on a port would be much welcomed ! I am willing to rewrite code as needed.

My code is not event driven. Not sure if this works better.

If any add'l info is needed, please let me know.

Edited by Myicq

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

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