TCP Send buffer

BlitzMax Forums/BlitzMax Programming/TCP Send buffer

King Dave(Posted 2006) [#1]
Is there any way in Max to find out how much more can be written to a TCP socket stream safely before the program will freeze until more data has been read by the client?

Below is an example of the problem, build it and then run it twice.

SuperStrict
Framework BRL.SocketStream
Import BRL.StandardIO
Import BRL.PolledInput

Local a%,b%
Local Server:TSocket=CreateTCPSocket(),Client:TSocket
Local ServerSt:TSocketStream,ClientSt:TSocketStream

If Not BindSocket(Server,1320)
	Print "Client mode..."
	
	CloseSocket Server
	Server=Null
	
	Client=CreateTCPSocket()
	If Not ConnectSocket(Client,HostIp("localhost"),1320) Then RuntimeError "Failed to connect to "+DottedIP$(HostIp("localhost"))+":1320!"
	
	ClientSt:TSocketStream=CreateSocketStream(Client)
	
	Repeat
		Print SocketReadAvail(Client)+" avail..."
		Delay 500
	Until KeyHit(1) Or AppTerminate() Or Eof(ClientSt)
Else
	Print "Server mode..."
	If Not SocketListen(Server) Then RuntimeError "Failed to start listening on 1320!"
	
	Repeat
		Client=SocketAccept(Server)
		a=a+1
		If a>49
			Print "Waiting for connection..."
			a=0
		EndIf
		Delay 10
	Until Client
	Print "Connection recieved, sending data..."
	
	ServerSt:TSocketStream=CreateSocketStream(Server)
	ClientSt:TSocketStream=CreateSocketStream(Client)
	
	a=0
	Repeat
		For b=1 To 1024
			WriteByte ClientSt,a
			a=a+1
		Next
		Print "Sent: "+a+"..."
	Until KeyHit(1) Or AppTerminate() Or Eof(ClientSt)
EndIf

If ClientSt
	CloseStream ClientSt
	ClientSt=Null
	Client=Null
EndIf
If ServerSt
	CloseStream ServerSt
	ServerSt=Null
	Server=Null
EndIf

Print "Cleaned up"

End


Notice how the first program (the server) stops responding after sending 32,768 bytes (approx... might vary from system to system)?

This is because the second program (the client) isn't reading the data being sent to it.

Now you may wonder why a client might not be reading its data... well, it could be a malicous program trying to cause this very effect to your server, or it could just be a dead connection where the Eof hasn't kicked in yet.
If its a dead connection your server will carry on as soon as the connection is dropped. If its a malicious program doing it on purpose, it will remain frozen forever.



AFAIK there is no way to find out how many bytes you can safely write to a stream before this will happen... I'm not even sure if its possible to find out in other languages. Does anyone know?

Thanks!


Wings(Posted 2008) [#2]
The workaround i have.

is to have static packet size.

and try to kep trafic as low as possible.

Try to kep a que system in server..