Jump to content
Sign in to follow this  

TCPRecv issue

Recommended Posts

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:


While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $Accept = -1 Then
            $msg = GUIGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then 
            $Accept = TCPAccept($RecvSocket)
        Until $Accept <> -1
    $tcpReceived = TCPRecv($Accept,1024*32)
    If $tcpReceived <> '' Then
    ;$tcpReceived = _HexToString($tcpReceived)
    ;$tcpReceived = _StringEncrypt(0,$tcpReceived,'253E7E60',1)
        $Accept = -1


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)
        $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)
    $Login = -1

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


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


$data = ''
    $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()?


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  

  • Create New...