Sign in to follow this  
Followers 0
cjconstantine

TCPRecv issue

5 posts in this topic

Ok, here's a strange problem I've run into ...

I'm sending a string with TCP, it's about 4100 bytes in length. The TCPRecv buffer is set to 32768, but for some reason it won't receive all the data all the time. TCPSend reports the right size being sent and the size sent is consistant. But sometimes I'll receive all the data, sometimes I recieve a percentage of it and once I got some of the beginning and some of the end with a chunk of middle missing.

I've tried upping the TCPTimeOut option and that didn't work. Here's some code:

Receiver:

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    
    If $Accept = -1 Then
        Do
            $msg = GUIGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then 
                TCPShutdown()
                Exit
            EndIf
            $Accept = TCPAccept($RecvSocket)
        Until $Accept <> -1
    EndIf
    
    $tcpReceived = TCPRecv($Accept,1024*32)
    If $tcpReceived <> '' Then
    ;$tcpReceived = _HexToString($tcpReceived)
    ;$tcpReceived = _StringEncrypt(0,$tcpReceived,'253E7E60',1)
    ;Sleep(5000)
        GUICtrlSetData($edit,$tcpReceived)
        TCPCloseSocket($Accept)
        $Accept = -1
    EndIf
WEnd

Sender:

Func _MonitorLogin()
    $tcpRecvID = StringRight($tcpRecvID,StringLen($tcpRecvID)-4)
    $tcpRecvID = _StringEncrypt(0,$tcpRecvID,'253E7E60',1)
    $LoginCount += 1
    ReDim $Remote[$LoginCount+1][6]
    $Remote[$LoginCount][1] = $tcpRecvID
    $Remote[$LoginCount][2] = 0
    $Remote[$LoginCount][3] = @MON&'/'&@MDAY&'/'&@YEAR&' '&@HOUR&':'&@MIN&':'&@SEC
    $Remote[$LoginCount][4] = 'User Name'
    $InitData = Call('_PrepDataSend',$LoginCount)
;$InitData = _StringToHex(Call('_PrepDataSend',$LoginCount))
;$InitData = _StringEncrypt(1,Call('_PrepDataSend',$LoginCount),'253E7E60',1)
    Do
        $tcpSend = TCPConnect($Remote[$LoginCount][1],42721)
    Until $tcpSend <> -1
    $Size = TCPSend($tcpSend,$InitData)
    $Remote[$LoginCount][5] = @MON&'/'&@MDAY&'/'&@YEAR&' '&@HOUR&':'&@MIN&':'&@SEC
    GUICtrlCreateListViewItem($Remote[$LoginCount][4]&'|'&$Remote[$LoginCount][3]&'|'&$Remote[$LoginCount][1]&'|All Server Data|'&Round($Size/1024,0)&' KB'&'|'&$Remote[$LoginCount][5],$lvMonitors)
    TCPCloseSocket($Login)
    $Login = -1
EndFunc

There is an edit box on the receiving end to show me what data was received. Using the Sleep line in the receiver didn't help either.

Share this post


Link to post
Share on other sites



I bet you've sent at least a shazzamabyte or two in your lifetime...


Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I don't have time to look at it... but TCP data is buffered. You loop until you receive all of the data. I always preface my data packet with the length of the packet, so that my script know when it has received all the data... I have never experienced "missing" data. And I have sent billions of bytes.

LAr.

Thank you LAr! You got me pointed in the right direction.

:whistle:

$data = ''
Do
    $tcpReceived = TCPRecv($Accept,1024)
    $data &= $tcpReceived
Until $tcpReceived = ''

This did the trick. How do you implement the packet length? The amount of data I send will be varied in size.

Edited by cjconstantine

Share this post


Link to post
Share on other sites

That's strange... I've never seen this happen and I've written quite a few servers in Au3. It is my understanding that the buffer is defined at the function call:

$Data = TCPRecv($Socket,$BufferSize)

Maybe TCPSend is breaking up the data in the last few patches?

Quick little side-track:

Is there a better way to check for incoming data with multiple connections than spamming TCPRecv()?

-CMR

Share this post


Link to post
Share on other sites

On the TCPSend side you send the byte length first like...

TcpSend($sock,StringLen($packet) & "," & $packet)

or something...

Lar.

Thanks again!

Share this post


Link to post
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
Sign in to follow this  
Followers 0