TCP Buffer Size

BlitzMax Forums/BlitzMax Programming/TCP Buffer Size

Banshee(Posted 2007) [#1]
I'm having difficulty with a network application when there is a burst of data. I'm using a TCP socket and I believe the problem lies in the fact that the buffer is filling up and chopping packet off, so what I get afterwards is corrupted.

I've hunted through the help and searched and cannot find a way to change the size of the TCP buffer, have I missed it?

I'm using BMax+GUI module on this one, compiling under MacOS 10.4.10


Raph(Posted 2007) [#2]
I ran into this myself recently. I was getting cut off at 8192 bytes. Basically, SocketReadAvail() returns 8192 as a max even if there's more coming.

The code isn't on this machine, but from memory, the fix was to change "if SocketReadAvail(socket)" into "while SocketReadAvail(socket) > 0", and keep appending to the destination string.


Vertex(Posted 2007) [#3]
Local Size:Int

Size = 1% Shl 16 - 1 ' Maximum payload in TCP
If setsockopt_(Socket, SOL_SOCKET_, SO_RCVBUF_, Varptr(Size), 4) = SOCKET_ERROR_ Or ..
   setsockopt_(Self.Socket, SOL_SOCKET_, SO_SNDBUF_, Varptr(Size), 4) = SOCKET_ERROR_ Then Failed


But please make sure, you don't use original constants in Pub.StdC. These values are wrong for Linux and SOL_SOCKET is not even declared. So use this:
?Win32
	Const SOL_SOCKET_   : Int   = $FFFF
	Const SO_SNDBUF_    : Short = $1001
	Const SO_RCVBUF_    : Short = $1002

?MacOS
	Const SOL_SOCKET_   : Int   = $FFFF
	Const SO_SNDBUF_    : Short = $1001
	Const SO_RCVBUF_    : Short = $1002

?Linux
	Const SOL_SOCKET_   : Int   = 1
	Const SO_SNDBUF_    : Short = 7
	Const SO_RCVBUF_    : Short = 8
?


cu olli


Banshee(Posted 2007) [#4]
Sorry it's been a while since I was looking at this, I left the job where I was working with TCP on the Mac, went into hospital, and then wrote a PC application using TCP - with the same problem.

I'm looking at the code above Vertex, and i'm sorry to say I dont really understand where I should put this - it's not Becky-style code lol! So i'm having trouble understanding it.

The first snippet appears to be setting the size to 1.6m bytes, but where should I put this please?

The second snippet, is this a modification to a BRL file?

Thank you for any assistance you can give, the buffer problems are really causing me grief with a live application.


Vertex(Posted 2007) [#5]
If you have a BRL socket like MySocket:TSocket then you put this code after CreateTCPSocket:
Local Size:Int

Size = 1% Shl 16 - 1 ' Maximum payload in TCP
If setsockopt_(MySocket._socket, SOL_SOCKET_, SO_RCVBUF_, Varptr(Size), 4) = SOCKET_ERROR_ Or ..
   setsockopt_(MySocket._socket, SOL_SOCKET_, SO_SNDBUF_, Varptr(Size), 4) = SOCKET_ERROR_ Then 'Failed'
(you have to edit 'Failed' with some exception handling like Throw etc.)

And this code:

have to be placed in your code next to SuperStrict or so on. In Pub.StdC are the constants named SOL_SOCKET and in my modification there are named SOL_SOCKET_ so you don't have to edit some BRL or Pub modules.

Size = 1% Shl 16 - 1 Byte = 65535 Byte ~ 64 KByte is based on the TCP. See http://en.wikipedia.org/wiki/Transmission_Control_Protocol#TCP_Header and look for "Window"-field. It is a 16 Bit field. So the payload can have a size up to 2^16 - 1 Byte.

cu olli