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

Gregory Smith wolfy at treellama.org
Mon Oct 8 10:34:56 PDT 2007


On Mon, 8 Oct 2007, Alvin wrote:

> What I have done is send the number of bytes before hand. The receiver then
> knows how many bytes to expect. For example, the sender transmits a single
> integer (the number of bytes for the payload) which is expected by the
> receiver. The receiver gets the integer and uses that value to receive
> exactly those number of bytes for the second transmission (e.g. the payload).

This only works, of course, for protocols you invent yourself.

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

We use SDL_Net currently, but we go behind its back and make the sockets 
non-blocking in an ugly, platform specific way. I'm surprised no one here 
has responded with the stock "Use threads" response--but non-blocking 
reads are far easier and not much more inefficient than setting up a whole 
thread system.

The answer to the original question is, you can't, really :(

Gregory


More information about the SDL mailing list