[SDL] How to know when SDLNet_TCP_Recv has no more data available , or how to specify timeout for it?

David Olofson david at olofson.net
Mon Oct 8 11:23:22 PDT 2007


On Monday 08 October 2007, Gregory Smith wrote:
[...]
> > What I have done is send the number of bytes before hand. The
> > receiver then knows how many bytes to expect.
[...]
> This only works, of course, for protocols you invent yourself.

Another problem is that you're *still* using a blocking call, which 
means any network delays in the middle of a "packet" will stall your 
application anyway.


> > Another solution could be to have the receiver read one byte at a
> > time. The sender would then send a control byte (of some kind) to
> > indicate the end of the transmission.
> 
> I have to imagine that's really inefficient.

Probably not all that bad with a low volume protocol on a modern PC 
running a serious OS, but still not recommended.


[...]
> I'm surprised no one here has responded with the stock "Use threads"
> response--

Ok then; why not do the network I/O in a dedicated thread? ;-)

Well, there is one good reason to avoid that method, although it 
really is the proper way of doing it: Complexity. Whether you're just 
wrapping the calls to avoid blocking, or you're doing some real work 
(ie decoding, preparing "requests" for the game logic etc) in that 
thread as well, there has to be communication with other threads 
sooner or later, and that's where things tend to get a bit hairy.

Why not try Bob Pendleton's event based NET2 layer? Nice, efficient 
and easy to use. You get a proper solution without having to worry 
about your own code running in concurrent threads.
	http://gameprogrammer.com/net2/net2-1.html


> but non-blocking reads are far easier and not much more inefficient
> than setting up a whole thread system.

I suppose "efficient" is a relative term... :-)

If an application (a game server, maybe?) ends up doing a lot of 
polling in relation to the amount of real work done, it'll waste 
quite a bit of CPU time doing pointless syscalls. It gets worse with 
more dense traffic, higher "poll rates" (ie game frame rate) - and 
what do you do if the application has no natural heart beat (such as 
a video frame rate) to drive the polling?


//David Olofson - Programmer, Composer, Open Source Advocate

.-------  http://olofson.net - Games, SDL examples  -------.
|        http://zeespace.net - 2.5D rendering engine       |
|       http://audiality.org - Music/audio engine          |
|     http://eel.olofson.net - Real time scripting         |
'--  http://www.reologica.se - Rheology instrumentation  --'


More information about the SDL mailing list