[lug] [OT] Threads & Socket Programming
Scott Herod
herod at dimensional.com
Fri Oct 25 08:49:26 MDT 2002
Ok, I'll start interlacing my responses. :;-)
Mr Viggy wrote:
> Someone else may be able to correct me, but I thought sockets in C
> (C++) were non-blocking.
I believe that it depends on how the file ( pipe, socket, etc. ) is
opened. Typically a flag specified in the open or socket function call
sets the behavior.
>
> David Morris wrote:
<snip>
>>
>> Everything works fine, except that if I write too many
>> packets to the socket too fast, I loose data packets (never
>> received by the client side). I can solve this problem by
>> putting in a sleep(1) statement, but that kind of defeats
>> the purpose of having a thread to handle writing data to the
>> socket from the queue.
>
As an aside, it's my impression is that sleep and usleep are BAD THINGS
in standard linux kernels (at least through 2.2.*) because the
granularity of context switching is about 10 msec. (This caused me all
kinds of nightmares a year or so ago. ) I've not used any of the
real-time or soft real-time extensions nor tested with any of the 2.4.*
kernels. BTW, in some code that I am looking at to recall this I have
the following few lines.
// select seems to work better with gdb than usleep
// usleep( timeoutInMS/1000 );
struct timeval tv;
tv.tv_sec = 0; tv.tv_usec = timeoutInMS*1000;
select( 1, NULL, NULL, NULL, &tv );
(No idea why I said that.)
>>
>> Anyone know why I would be having this problem? Do I need
>> to call some function to poll the socket and find out if
>> data can be sent out yet, or if I have to wait (thought such
>> blocking was handled automatically).
>>
>> Thanks for any input you can give on solutions, or other
>> lists to ask.
>>
>> --David
>>
My impression, David, is that this is the reading code's problem. While
reads can be set to block or not, I almost always wrap a select call
around one as well so that I can, in a sense, poll. Check to see how
the reading code is opening the socket and whether it is doing any
polling. If you want, reply off-line. I've got a pretty good
collection of sample code for reading/writing using various connections.
Scott
herod at dimensional.com
More information about the LUG
mailing list