Re: IOCP with graceful close
- From: Len Holgate <Len.Holgate@xxxxxxxxxxx>
- Date: Wed, 17 Aug 2005 11:38:26 +0100
My server side uses IOCP model. When some condition is meet, I want to server send a Command to client and then closesocket. If I use non-graceful close, the client can't always receive the cmd before the socket is closed.
You should be able to call shutdown(SD_BOTH), set linger and then closesocket. However you probably really want the client to initiate the connection closure as it's the side that performs the 'active close' that goes into TIME_WAIT and it's best not to 'collect' TIME_WAIT sockets on your server side... So, I'd suggest the following; Send your last command and call shutdown(SD_SEND) to tell the client that you wont send any more data. The client gets the last command and acts on it, the next read the client issues returns 0 indicating that the server has shut down the send side. The client calls shutdown(SD_BOTH) and closesocket - no need to linger as there's no data on the wire. The server has an outstanding recv on the socket and this now completes and returns 0 to indicated that the client has closed the connection. The server calls closesocket and the connection is closed.
This should leave a socket in TIME_WAIT on the client and nothing on the server.
Our IOCP server framework is a little more complicated than that as we use write completion notifications to control when a send shutdown actually happens, see the code on my blog if you're interested.
-- Len Holgate - http://www.lenholgate.com JetByte Limited - http://www.jetbyte.com The right code, right now. Contract Programming and Consulting Services. .
- Follow-Ups:
- Re: IOCP with graceful close
- From: Sam Shi
- Re: IOCP with graceful close
- References:
- IOCP with graceful close
- From: Sam Shi
- IOCP with graceful close
- Prev by Date: IOCP with graceful close
- Next by Date: SOS SERVER and FTP SERVER
- Previous by thread: IOCP with graceful close
- Next by thread: Re: IOCP with graceful close
- Index(es):
Relevant Pages
|