Jump to content

Problems with TCP binary data transfering...


Glyph
 Share

Recommended Posts

First off, here is the code im creating a gui for: http://www.autoitscript.com/forum/index.ph...mp;hl=gtaspider

Now, here is my code with the failed adaptation:

SERVER:

#include <GUIConstantsEx.au3>
#include <GUIedit.au3>
#include <WindowsConstants.au3>
    Local $iMainSocket, $iAccSocket = -1, $sBuff, $sRecv = "", $i = 0
        TCPStartup()
    $iMainSocket = TCPListen(@IPAddress1, 63334)

    $sBuff = Binary ($sBuff); binary buffer (contains null data atm)
    
GUICreate("My GUI",400,400)  ; will create a dialog box that when displayed is centered
$hedit=GUICtrlCreateEdit("",0,0,400,400)
GUISetState(@SW_SHOW)       ; will display an empty dialog box

    ; Run the GUI until the dialog is closed
    While 1
        sleep(1)
        sockers()
     if  $iAccSocket = -1 then                      ;WAIT FOR A SOCKET TO CONNECT
        $iAccSocket = TCPAccept($iMainSocket)
        Sleep(49)
    EndIf
    
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd

func sockers()
        $sRecv = TCPRecv($iAccSocket, 8184, 1) ; when recieved, it is Binary data
        $sRecv = BinaryToString ($sRecv) ;convert the binary to an ascii string
        
    if $sRecv <> "" then ; if the tcprecv has incoming data then do this loop
    $split=StringSplit($sRecv,"|")
        If $split[1]="file"  Then ; if the ascii string has a comma in it and its the first loop then
            _GUICtrlEdit_AppendText($hEdit, @CRLF & "received data command")
        $sBuff &= $split[2] ; set the buffer to the ascii string (the file)
        _GUICtrlEdit_AppendText($hEdit, @CRLF & "appending to buffer")
        $sRecv = BinaryToString (TCPRecv($iAccSocket, 8184, 1))  ;set the incoming socket form binary to ascii
        _GUICtrlEdit_AppendText($hEdit, @CRLF & "reinitiating tcprecv")
        
        If @error Then 
            _GUICtrlEdit_AppendText($hEdit, @CRLF & "write()")
            write()
        EndIf
        
        EndIf
    EndIf
    
EndFunc

func write()
    _GUICtrlEdit_AppendText($hEdit, @CRLF & "Func Write(starting)")
    $sFileName="wee.exe"
;function to rename the file if there is the same existing filename
    If FileExists($sFileName) Then
        $sTmp = StringSplit($sFileName, ".")
        If $sTmp[0] < 2 Then
            While 1
                $i += 1
                If Not FileExists($sFileName & "(" & $i & ")") Then
                    $sFileName = $sFileName & "(" & $i & ")"
                    ExitLoop
                EndIf
            WEnd
        Else
            While 1
                $i += 1
                If Not FileExists($sTmp[1] & "(" & $i & ")" & $sTmp[2]) Then
                    $sFileName = $sTmp[1] & "(" & $i & ")." & $sTmp[2]
                    ExitLoop
                EndIf
            WEnd
        EndIf
    EndIf
    
;Write the file!
    $iFileOp = FileOpen($sFileName, 16 + 2)
    If @error Then Return SetError(2,0,-1)
    FileWrite($iFileOp, $sBuff) ; write the file from the data in the buffer
    If @error Then Return SetError(3,0,-1)
    FileClose($iFileOp)
    TCPCloseSocket($iAccSocket)
    TCPShutdown()
    Exit
EndFuncoÝ÷ Ù° CSý¶®¶­sb6æ6ÇVFRÖöæ6P¢ôfÆU6VæBgV÷C¶3¢b3#¶6ÖBæWRgV÷C²ÄFG&W73£³ÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓУ°£²gVæ7FöâæÖS¢ôfÆU6Væ@£²FW67&Fö㣢6VæFærfÆRFòâ6W'fW"6VRôfÆU&V6VfR£²&ÖWFW"2¢£²b33c·4fÆR¢FRfÆRFò6Væ@£²b33c´¢FRF&vWBG&W70£²b33cµõ%B´÷FöæÅÒ¢FR÷'BFòW6R×W7B&RFR6ÖR2'ôfÆU&V6WfRb333²b333²£²b33c¶7Æ6´÷FöæÅÒ¢FVfVÂÒ6÷r7Æ6FW@£²ÒFöçB6÷r7Æ6FW@£²b33c¶vEvÆT6öææV7FVB´÷FöæÅÒ¢ÒbD56öææV7BfÆBW'&÷"&WGW&âvFW'&÷"6öFR £²FVfVÇBÒbD56öææV7BfÆBWBBâvÆRVçFÂ6öææV7FV@£²&WV&VÖVçB2¢Ð£²&WGW&âfÇVR2¢öâ7V66W2&WGW&⣲öâfÆB&WGW&âÓæBW'&÷"6öFRÓ3 £²W'&÷"Ò¢fÆRæ÷BW7@£²W'&÷"Ò#¢D56öææV7BfÆBöæÇ&ÆRbb33c¶vEvÆT6öææV7FVBfÇC²fwC²£²W'&÷"Ò3¢fÆBFò÷VâfÆP£²W'&÷"ÒC¢6öææV7Föç&ö&ÆVÒvÆR6VæFæp£²WF÷"2¢wF7FW £°£³ÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓТ¤gVæ2ôfÆU6VæBb33c·4fÆRÂb33c´Âb33cµõ%BÒc333BÂb33c¶7Æ6ÓÂb33c¶vEvÆT6öææV7FVBÒ¢Æö6Âb33c¶Öå6ö6¶WBÒÓÂb33c·4'VfbÂb33c¶fÆT÷Âb33c·5&V7`¢¢bæ÷BfÆTW7G2b33c·4fÆRFVâ&WGW&â6WDW'&÷"ÃÂÓ¢bb33c¶7Æ6FVâb33c¶7Æ6Ò7Æ6FWDöâb33²b33²Âb33µG'Fò6öææV7Bâââb33²Ã#Ã#ÂÓÂÓâD57F'GW¢¢bçVÖ&W"b33c¶vEvÆT6öææV7FVBÒFVà¢vÆRb33c¶Öå6ö6¶WBÒÓ¢b33c¶Öå6ö6¶WBÒD56öææV7Bb33c´Âb33cµõ%B¢tVæ@¢VÇ6P¢b33c¶Öå6ö6¶WBÒD56öææV7Bb33c´Âb33cµõ%B¢bW'&÷"FVâ&WGW&â6WDW'&÷""ÂÂÓ¢VæD`¢bb33c¶7Æ6FVâ6öçG&öÅ6WEFWBb33c¶7Æ6Âb33²b33²Âb33µ7FF3b33²Âb33µ&VBfÆRâââb33²¢b33c¶fÆT÷ÒfÆT÷Vâb33c·4fÆRÂb¢bW'&÷"FVâ&WGW&â6WDW'&÷"2ÃÂÓ¢b33c·4'VfbÒfÆU&VBb33c¶fÆT÷¢fÆT6Æ÷6Rb33c¶fÆT÷¢bb33c¶7Æ6FVâ6öçG&öÅ6WEFWBb33c¶7Æ6Âb33²b33²Âb33µ7FF3b33²Âb33µ6VæFærfÆRâââb33² ¢vÆR&æ'ÆVâb33c·4'Vfb¢b33c¶6VæE&WGW&âÒD56VæBb33c¶Öå6ö6¶WBÂ&æ'gV÷C¶fÆWÂgV÷C²fײb33c·4'Vfb¢bW'&÷"FVâ&WGW&â6WDW'&÷"BÂÂÓ¢G&Fb33²b33²Âb33c¶6VæE&WGW&ââb33c·4'VfbÒ&æ'ÖBb33c·4'VfbÂb33c¶6VæE&WGW&â²Â&æ'ÆVâb33c·4'VfbÒb33c¶6VæE&WGW&â 6ÆVW¢tVæ@ ¢bb33c¶7Æ6FVâ6öçG&öÅ6WEFWBb33c¶7Æ6Âb33²b33²Âb33µ7FF3b33²Âb33µ7V66W6gVÆÂb333²b33²¢D56Æ÷6U6ö6¶WBb33c¶Öå6ö6¶WB¢D56WFF÷vâ¢6ÆVW¢7Æ6öfb¢&WGW&â¤VæDgVæ0

The problem is with the server, I can't seem to get it to keep looping to accept data constantly from the client.

Since tcprecv only goes to x bytes, I wanted to create a workaround to keep accepting data, and adding it to a buffer.

That will then, write the file after all data has been received.

The client sends ok.

I know this because, if I set the tcprecv to 200k bytes the 100kb file sends perfectly.

Now I added the loop to the server so I can keep accepting data, but i'm having problems!

Thank you very much, for reading this over. I hope that you can be of help!!

tolle indicium

Link to comment
Share on other sites

I am doing some binary TCPReceive as well.

In principle you must keep polling the TCP stack until and append the received part to the buffer. Otherwise the stack might sop receiving due to buffer overflow...

global eof=0

TCPconnectstuff

while 1

do $filestring=$filestring&readstack() until eof=1

if eof=1 then filewrite(bintostring($filestring)

wend

func readstack()

$recv=TCPreceive($connection, 10240,1)

if stringinstring("|EOFreached|",binarytostring($rec)) then eof=1

endfunc

then you need to send your filestring attached with &"|EOFreached|".

Works for me. If you want to slo down a bit - do not use sleeps (it will slow down unneeded).

Just use a timerinit before the main while loop and use timerdiff in readstack(). Then poll i.e. each 100msec only (if timerfdiff>100 then $recv=TCPReceive... this will cause the local TCP stack buffer to fill up and signalize the remote sender to slow down (window size decrement) autonomously.

Also keep in mind that the operation of bin<->asc or any string operation on a 1Mbyte string is taking much time to process - so it should not be done in the receive loop but somewhere when the complete filestring is received or on the partial strings only.

Hope this helps a bit!

Daniel

Edited by ds34
Link to comment
Share on other sites

I am doing some binary TCPReceive as well.

In principle you must keep polling the TCP stack until and append the received part to the buffer. Otherwise the stack might sop receiving due to buffer overflow...

global eof=0

TCPconnectstuff

while 1

do $filestring=$filestring&readstack() until eof=1

if eof=1 then filewrite(bintostring($filestring)

wend

func readstack()

$recv=TCPreceive($connection, 10240,1)

if stringinstring("|EOFreached|",binarytostring($rec)) then eof=1

endfunc

then you need to send your filestring attached with &"|EOFreached|".

Works for me. If you want to slo down a bit - do not use sleeps (it will slow down unneeded).

Just use a timerinit before the main while loop and use timerdiff in readstack(). Then poll i.e. each 100msec only (if timerfdiff>100 then $recv=TCPReceive... this will cause the local TCP stack buffer to fill up and signalize the remote sender to slow down (window size decrement) autonomously.

Also keep in mind that the operation of bin<->asc or any string operation on a 1Mbyte string is taking much time to process - so it should not be done in the receive loop but somewhere when the complete filestring is received or on the partial strings only.

Hope this helps a bit!

Daniel

This helps alot.

Thank you very much, I appreciate the tips!

tolle indicium

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