Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to detect FD_CLOSE socket event with IOCP mechanism?

304 views
Skip to first unread message

Ivan Misuno

unread,
Feb 4, 2002, 6:15:39 AM2/4/02
to
Hello, All

Is there a way to detect non-overlapped socket events along with
using IOCP notification mechanism to send/receive data?
I can't use WSAEventSelect() and wait in MsgWaitForMultipleObjectsEx()
to detect them, because there will be a lot more sockets than
MsgWaitForMultipleObjectsEx() can accept handles to wait (MAXIMUM_WAIT_OBJECTS-1).
Also, I don't want to create multiple threads so that each will wait on limited
number of events.

What are options here?

Currently I can think only of periodical scanning of all open sockets, which
would not provide responsive event detection...

--
Regards,
Ivan Misuno


Ivan Misuno

unread,
Feb 4, 2002, 10:18:55 AM2/4/02
to

I can only add, that in case of normal socket shutdown by client,
GetQueuedCompletionStatus() returns with no error, indicating only
that dwBytesTransfered == 0, returning correct LPOVERLAPPED pointer,
by which I can dispatch event to correct handler.

Is it correct to compare obtained dwBytesTransfered with zero,
in which case it will indicate that socket is to be closed?

--
Regards,
Ivan Misuno


Grant Parry

unread,
Feb 5, 2002, 10:24:29 AM2/5/02
to
Ivan,

We have been using this approach with good success.

bResult = GetQueuedCompletionStatus(
Client.ghCompletion,
&dwNumRead,
&dwIndex,
&lpOverlapped,
INFINITE
);

if (bResult == FALSE && lpOverlapped != NULL) {
// This happens occasionally instead of
// end-of-file. Not sure why.
closesocket(dwIndex);
releaseConnection(dwIndex);
} else if (dwNumRead == 0) {
closesocket(dwIndex);
releaseConnection(dwIndex);


The problem we are having, however, is that we don't get
any notification of the socket closing if there is no read
pending.

We have a server application that monitors up to 500
incoming sockets. Once we get a request, we don't post
the next read on the socket until the current request has
been completed.

We are running into a situation where the client processes
are sending in requests, and then timing out and closing
the socket. Their timers are extremely short, and it is
inevitable that some of the requests will time out. The
problem we are having is that we don't know that they have
timed out and closed the socket, because we don't have a
read pending on the socket -- therefore we don't get
ANYTHING from GetQueuedCompletionStatus at all.

I've been searching for a way to find out if the socket is
still open, but haven't been able to find anything in the
documentation. Is there anyone out there that has solved
this problem, or knows what I can try?

Thanks,

Grant

>.
>

John Duddy

unread,
Feb 5, 2002, 1:01:12 PM2/5/02
to
It sounds like you are mixing paradigms.

IOCP is intended to allow the OS to keep the threads servicing your sockets
running continuously for maximum throughput OF THE SOCKETS. It looks like
you are trying to handle complete, application-level transactions within the
context of a single one of your GetQueuedCompletionStatus result processing.
It's one thing to do operations that block because you can set up the IOCP
to release another thread to take up the slack and keep servicing the
sockets. But turning around and doing non-overlapped things with your
sockets in an overlapped completion thread defeats the purpose.

You might want to have your app logic fed from an auxiliary queue, which is
populated by the IOCP threads. Then you can have reads pending continuously,
and closure of a socket will be detected and placed in the queue for
processing along with the rest of the data. With a good reader/writer lock
favoring the IOCP threads, perhaps with a cap on the number of queue
entries, you should be able to get some good throughput with minimal IOCP
thread blocking.

Just my $1.52
JD
"Grant Parry" <grant...@psi-cu-software.com> wrote in message
news:029701c1ae59$34170210$a5e62ecf@tkmsftngxa07...

Darren

unread,
Feb 7, 2002, 3:59:05 PM2/7/02
to
Hi all,

I'm in exactly the same position as Ivan, having written a web proxy server
(with a pool of 500 sockets which have AcceptEx called), and noticed that
some connected sockets dont receive completion notifications - presumably
due to the browser making requests and then closing the socket (reproducable
by hitting the refresh button many times).

I need to detect remote socket closure - FD_CLOSE - and post a notification
to the completion port so the socket can be recycled.
But whats the best method to do this?

I toyed with the idea of using WSAEventSelect() & WaitForSingleObject(),
registering an interst in FD_CLOSE with all 500 sockets using a single
event. However once my event got signalled I would need to identify which
socket had been closed.. Hmmm.. i wouldn't want to iterate/poll each
socket, even if i knew how to detect a closed socket without using the
traditional recv()=0?

Any advice welcome please!

Many thanks,
Darren

"Ivan Misuno" <nospam@please> wrote in message
news:u8rJO1WrBHA.2524@tkmsftngp03...

arkadyf

unread,
Feb 10, 2002, 10:53:09 AM2/10/02
to
Completely agree with John and my 2 c .
Why not use KEEPALIVE ( HEARTBEAT or so ever ) logic to see if socket alive
without any connection to IOCP logic which born to supply some predefined
number of threads serve some ( much more than threads ) number of
I/Os ( sockets )
Arkady

John Duddy <jdu...@idontwantyourunsolicitedmsgDOTlogicman.com> wrote in
message news:u6078tc...@corp.supernews.com...

0 new messages