Re: Handling multiple connections
- From: "Alexander Nickolov" <agnickolov@xxxxxxxx>
- Date: Tue, 28 Feb 2006 12:40:42 -0800
It's actually very simple, at least in theory. Redesign your
function to not assume it will read the data in one call. This
is called event-driven programming and has been around
for more than 20 years... Basically, remember the state for
each connection and read all available data from the socket
each time select says the socket has more data. If you have
the data ready, you can forward it to a higher layer for
processing, and not necessarily from within that same
function - here's where it makes sense to have another
thread pick it up. If not, simply return without alerting your
processing thread.
Still, I do want to point out that select cannot scale beyond
64 concurrent connections in Windows... This may or may
not be an issue for you, I don't know.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"Søren Dreijer" <bleh@xxxxxxxx> wrote in message
news:%23IbyCN7OGHA.3360@xxxxxxxxxxxxxxxxxxxxxxx
Yesterday I realized a possible design flaw in my network library.
Currently, I have a single thread dedicated to handle receives from all
clients connected to my server. I use select() to see if new data has been
received, and then I loop through each socket and receive the actual data.
All data being sent by clients are prepended with a header that indicates
if
the data is compressed and the length of the data. I've made a function
which first reads the header, then reads the specified amount of bytes (as
indicated by the header). That is, the function doesn't return until
everything has been received.
However, I came to think about what happens if the header is received and
the client then disconnects or the transfer is mighty slow. The receiving
thread would then hang until the socket times out or all the data has been
received and thus delays receiving on the other readable sockets.
I see two solutions to this problem:
- When the client closes the connection, the recv() call fails instantly
(no
timeout) and I can continue with the other sockets. This doesn't solve the
problem of slow transfers, though. The thread would still wait for all
data
to be received before proceeding to the next readable socket.
- Creating a thread for each client connected to the server. This thread
is
then responsible for receiving only on that single socket. One drawback is
the number of context switches taking place when the number of clients
connected increase, but I'm not sure how big an impact this will have on
performance really.
Also, having a thread take care of a single socket makes it easier to do
more direct error handling specific to that socket.
What would you guys suggest?
.
- Follow-Ups:
- Re: Handling multiple connections
- From: Søren Dreijer
- Re: Handling multiple connections
- References:
- Handling multiple connections
- From: Søren Dreijer
- Handling multiple connections
- Prev by Date: Re: How can i force sending ACK after receiving FIN from server while close session?
- Next by Date: Re: Handling multiple connections
- Previous by thread: Re: Handling multiple connections
- Next by thread: Re: Handling multiple connections
- Index(es):
Relevant Pages
|
Loading