how to detect client remotely closing socket with IO Completion Ports, WSA ASync calls?

how to detect client remotely closing socket with IO Completion Ports, WSA ASync calls?

Post by Darre » Fri, 08 Feb 2002 07:42:48



Hi,

Q. How can you detect when a socket has been closed remotely, when using
IOCP?

I know that with tradition blocking sockets you perform a ::recv which
returns 0 if the socket has been remotely closed.
I dont want to do this because the non-blocking socket i am checking already
has an outstanding WSASend/WSARecv operation, for which no completion has
been received (for quite some time).

______Context______
Ive written a web proxy using IOCP, it contains a pool of listening sockets.
My code is loosley based on the article:  "Windows Sockets 2.0: Write
Scalable Winsock Apps Using Completion Ports" from
http://msdn.microsoft.com/msdnmag/issues/1000/Winsock/Winsock.asp

If i continually hit refresh on my browser - generating a lot of proxy
requests, i've noticed some sockets on my proxy never seem to recieve
completion notifications, indefinitely.   I put this down to the browser
closing sockets (on which a request has been made but a response has not yet
been recieved), and opening new sockets upon which to make the "refresh"
requests.

I think my problem is that completion ports only generate notifications for
connect/send/recv events, and not for remote socket closure.
Most documentation leans towards calling WSAEventSelect(skt, event,
FD_CLOSE) for each socket, and then WaitForSingleObject() with an event.
Thats fine, but how would i resolve which socket generated the FD_CLOSE
event?

Thanks for your time,
Darren

 
 
 

how to detect client remotely closing socket with IO Completion Ports, WSA ASync calls?

Post by Douglas Peterso » Fri, 08 Feb 2002 12:27:15


Check out the sockhim program found here:
http://www.mvps.org/win32/index.html

It's under "Sockets, IOCPs, AcceptEx" in the index.

I've used that as a model to create a server and (as yet) have not had any
problems detecting closure.  Let me know what you think as I'm still pretty
green.


Quote:> Hi,

> Q. How can you detect when a socket has been closed remotely, when using
> IOCP?

> I know that with tradition blocking sockets you perform a ::recv which
> returns 0 if the socket has been remotely closed.
> I dont want to do this because the non-blocking socket i am checking
already
> has an outstanding WSASend/WSARecv operation, for which no completion has
> been received (for quite some time).

> ______Context______
> Ive written a web proxy using IOCP, it contains a pool of listening
sockets.
> My code is loosley based on the article:  "Windows Sockets 2.0: Write
> Scalable Winsock Apps Using Completion Ports" from
> http://msdn.microsoft.com/msdnmag/issues/1000/Winsock/Winsock.asp

> If i continually hit refresh on my browser - generating a lot of proxy
> requests, i've noticed some sockets on my proxy never seem to recieve
> completion notifications, indefinitely.   I put this down to the browser
> closing sockets (on which a request has been made but a response has not
yet
> been recieved), and opening new sockets upon which to make the "refresh"
> requests.

> I think my problem is that completion ports only generate notifications
for
> connect/send/recv events, and not for remote socket closure.
> Most documentation leans towards calling WSAEventSelect(skt, event,
> FD_CLOSE) for each socket, and then WaitForSingleObject() with an event.
> Thats fine, but how would i resolve which socket generated the FD_CLOSE
> event?

> Thanks for your time,
> Darren


 
 
 

how to detect client remotely closing socket with IO Completion Ports, WSA ASync calls?

Post by Darre » Sat, 09 Feb 2002 06:37:44


Hi Douglas, and all,

I had a look at that example and it doesnt seem to deal with unexpected
remote socket closure :-(
Meaning, it doesnt use WSAEventSelect or WSAAsyncSelect to detect when
clients "unexpectedly" close their end of the socket connection.

Under typical web proxy operating conditions I dont have a problem, eg:
A browser connects to my proxy, sends a HTTP request, proxy recieves
request, retrieves page from server, sends to client, and then disconnects
socket.

It's when a browser connects, send the http request and then closes the
socket without waiting for a reply.  I can reproduce this by pressing the
refresh button
many times quickly.  I get all the connect/receive notifications, but after
the proxy has retrieved the web page and then trys to WSASend() it to the
client i DONT receive a completion, and no error either!

Please anyone, whats the best method of detecting remote socket close, when
using IOCP with a pool of 500 (or greater) listening sockets?
Surely not an event per socket, because that would cost me 500/64 threads (1
thread per 64 event handles) to monitor all those events?

If i register interest in FD_CLOSE for all the sockets using the same
WSAEvent, how would i "efficiently" identify the socket which had been
closed from the single event?  Surely not by calling WSAEnumNetworkEvents()
for each of my connected sockets?

Please any advice would be very welcome,  thanks,
Darren


> Check out the sockhim program found here:
> http://www.mvps.org/win32/index.html

> It's under "Sockets, IOCPs, AcceptEx" in the index.

> I've used that as a model to create a server and (as yet) have not had any
> problems detecting closure.  Let me know what you think as I'm still
pretty
> green.



> > Hi,

> > Q. How can you detect when a socket has been closed remotely, when using
> > IOCP?

> > I know that with tradition blocking sockets you perform a ::recv which
> > returns 0 if the socket has been remotely closed.
> > I dont want to do this because the non-blocking socket i am checking
> already
> > has an outstanding WSASend/WSARecv operation, for which no completion
has
> > been received (for quite some time).

> > ______Context______
> > Ive written a web proxy using IOCP, it contains a pool of listening
> sockets.
> > My code is loosley based on the article:  "Windows Sockets 2.0: Write
> > Scalable Winsock Apps Using Completion Ports" from
> > http://msdn.microsoft.com/msdnmag/issues/1000/Winsock/Winsock.asp

> > If i continually hit refresh on my browser - generating a lot of proxy
> > requests, i've noticed some sockets on my proxy never seem to recieve
> > completion notifications, indefinitely.   I put this down to the browser
> > closing sockets (on which a request has been made but a response has not
> yet
> > been recieved), and opening new sockets upon which to make the "refresh"
> > requests.

> > I think my problem is that completion ports only generate notifications
> for
> > connect/send/recv events, and not for remote socket closure.
> > Most documentation leans towards calling WSAEventSelect(skt, event,
> > FD_CLOSE) for each socket, and then WaitForSingleObject() with an event.
> > Thats fine, but how would i resolve which socket generated the FD_CLOSE
> > event?

> > Thanks for your time,
> > Darren

 
 
 

how to detect client remotely closing socket with IO Completion Ports, WSA ASync calls?

Post by Sender » Sat, 09 Feb 2002 10:07:01


<<<I dont want to do this because the non-blocking socket i am checking
<<<already
<<<has an outstanding WSASend/WSARecv operation, for which no <<<completion
has
<<<been received (for quite some time).

Dude, the only time you are suppost to check the status of sockets in an
IOCP server is when you have outstanding AcceptEx's with a receive buffer
greater than zero, you use the getsockopt with SO_CONNECT_TIME ( i thinks ),
You do this to prevent stale connections that connect but don't send any
data!

You are NOT support to poll sockets in an IOCP server otherwise!!!!!!!

The whole point of IOCP is that it will let you know when anything happened
on a socket or pipe ect...,

IE. WSARECV completes with 0 bytes = graceful shutdown or closure.

If you do not get an completion for a outstanding call when the client
closes out, you are doing something wrong in you code!

 
 
 

1. WSA IO completion routines

When using IO completion routines am I stuck with the completion routine
executing in the same thread that originally posted it? I know in winsock
there are events (won't mention wnd msgs) but is there a way to have
completion port type of functionality under 98? What I want is a pool of
threads waiting on IO events and executing tasks *efficiently*.

Thanks

-Alen

2. has anybody got the DDD debugger to properly work on 64 bit apps?

3. Asyncronous socket IO without Completion Ports

4. refer Format to BibTeX format

5. Overlapped IO, Sockets, Completion Ports

6. Attaching SCSI drives

7. IO completion ports on UDP socket?

8. OnUpdateCmdUI in a dialog

9. Question on IO Completion ports, Sockets, and ReadFile/WriteFile

10. Datagram Sockets, WSARcvFrom, and IO Completion Ports

11. Problem with IOCP (Io completion port) used for serial port communication

12. WSA IO with IOCPs depends on calling thread!!?

13. Server app always return INVALID_SOCKET in accept call, after client closes the socket ?