3

Is it possible to initiate a TCP connection request with overlapped I/O, and cancel it before the connection has been completed in Windows? I need to support at least Windows XP SP2.

Jörgen Sigvardsson
  • 4,626
  • 2
  • 24
  • 50

2 Answers2

6

ConnectEx allows an overlapped connection attempt.

To cancel this one would need to use CancelIo passing the SOCKET as if it were a HANDLE (it is really). But this must be done from the same thread that called ConnectEx. Managing things so you can achieve that thread specificity is unlikely to be easy.

After XP/2003 (ie. Vista/2008/8/2008R2) you can use CancelIoEx from a different thread (the OVERLAPPED instance is used to fully identify the IO operation).

Richard
  • 100,436
  • 21
  • 189
  • 251
  • You could probably also cancel that connect attempt simply by closing the socket; the ConnectEx() will then fail... – Len Holgate Oct 10 '11 at 15:46
  • @Richard: Awesome, thanks! I will be cancelling from the same thread that issued the call to ConnectEx(), so this won't be a problem. But, does anybody know what happens on TCP-level? Suppose SYN has been sent, will cancelling the connect send an RST to the other side, or will it keep the handshake hanging? – Jörgen Sigvardsson Oct 10 '11 at 20:02
0

From here:

overlap

This directory contains a sample server program that uses overlapped I/O. The sample program uses the AcceptEx function and overlapped I/O to handle multiple asynchronous connection requests from clients effectively. The server uses the AcceptEx function to multiplex different client connections in a single-threaded Win32 application. Using overlapped I/O allows for greater scalability.

LordDoskias
  • 3,031
  • 3
  • 26
  • 40