ReceiveNetworkData
Posted: Sat Jul 05, 2008 4:25 am
I'm assuming this'll work as intended, I'm yet to test it, so feel free to edit.
Most of the examples I've seen regarding ReceiveNetworkData don't appear to take into account receiving large data streams and either assume your getting a tiny amount of data or they have an unnecessary propensity (habit) of Reallocating memory in case your getting more.
Also i think the default recv buffer in windows is 4096 bytes unless you set it to some other size.
Please correct me if I'm wrong, lectures most welcome!
senddata

Most of the examples I've seen regarding ReceiveNetworkData don't appear to take into account receiving large data streams and either assume your getting a tiny amount of data or they have an unnecessary propensity (habit) of Reallocating memory in case your getting more.
Also i think the default recv buffer in windows is 4096 bytes unless you set it to some other size.
Please correct me if I'm wrong, lectures most welcome!
Code: Select all
Procedure Listen(Dummy.l)
Protected TotalBytesRead.l,BytesRead.l,*DataBuffer, bufferlen.l, bigBufferlen.l, Timeout.l,err
bufferlen = 4096
bigBufferlen = 1024*1024 ;set to some max, you may need and if you need more it'll realocate appending the bufferlen
*DataBuffer = AllocateMemory(bufferlen)
*BigBuffer = AllocateMemory(bigBufferlen)
While 1
EventType = NetworkServerEvent()
If EventType = #PB_NetworkEvent_Data
TotalBytesRead = 0
Timeout = GetTickCount_() + 5000
Repeat ;
BytesRead = ReceiveNetworkData(EventClient(), *DataBuffer, bufferlen)
If BytesRead <> -1
If TotalBytesRead + BytesRead >= bigbufferlen
bigBufferlen + bufferlen
*tB = ReAllocateMemory(*BigBuffer,bigBufferlen)
If *tB
*BigBuffer = *tb
Else
;better wave a magic wand and flush your buffer your out of memory
EndIf
EndIf
CopyMemory(*DataBuffer, *BigBuffer + TotalBytesRead, BytesRead)
TotalBytesRead + BytesRead
Timeout = GetTickCount_() + 5000 ;increment the time out here in case loads of WSAEWOULDBLOCK = 10035
Else
err = WSAGetLastError_()
If err <> 10035 ;10035 temp unavailable keep trying
Debug "recv err = " + Str(WSAGetLastError_())
;look here for err descriptions http://msdn.microsoft.com/en-us/library/ms740668(VS.85).aspx
;Additionally call an error routine to determine if the error is recoverable and return the timeout else return 0
;timeout = LookupWSAError(err,timeout)
EndIf
EndIf
Until BytesRead = 0 Or GetTickCount_() > timeout
If BytesRead = 0
;do stuff with your data
Else
;it timed out lookup err and take some action
EndIf
EndIf
Delay(5)
Wend
FreeMemory(*DataBuffer)
FreeMemory(*BigBuffer)
EndProcedure
Code: Select all
Procedure SendData(ip,port,*data,len)
Protected Tx.l, Timeout.l, Connection.l
Connection = OpenNetworkConnection(ip, port)
If Connection
Tx = 0
Timeout = GetTickCount_() + 5000
Repeat
Result = SendNetworkData(Connection,*data+Tx,len-Tx)
If Result <> -1
Tx = Tx + Result
Timeout = GetTickCount_() + 5000
Else
err = WSAGetLastError_()
If err <> 10035
;timeout = LookupWSAerror(err,timeout)
;lookup error return timeout if recoverable else 0 and bail
Debug Str(err)
Endif
EndIf
Until Tx >= len Or GetTickCount_() > Timeout
CloseNetworkConnection(Connection)
EndIf
EndProcedure
