1

I've made a simple dummy server/dummy client program using IOCP for some testing/profiling purpose. (And I also wanted to note that I'm new to asynchronous network programming)

It looks like the server works well with original client, but when the dummy client tries to connect to the server with ConnectEx function, IOCP Worker thread still gets blocked by GetQueuedCompletionStatus function and never returns result while the server succeeds in accepting the connection.

What is the problem and/or the reason, and how should I do to solve this problem?

summerlight
  • 882
  • 7
  • 16
  • Please show more code. Anyway, what does it do when you try to telnet your server? (telnet localhost , very useful as a simple tcp client). – Pavel Radzivilovsky Jun 26 '10 at 11:40
  • I feel sorry that I can not show you actual code because it's based on our company's own library, but client's skeleton can be described like below: WSAstartup -> Create IOCP -> Initiate Worker Thread -> Get ConnectEx with IOCtl -> Bind socket -> Call ConnectEx -> Allocate Socket to IOCP. And I get "Socket is already connected." error with WSAGetLastError when I try to connect to the server repeatedly. I'll try to telnet my server tommorow. Thank you! – summerlight Jun 27 '10 at 05:24
  • It seems that it works properly when ConnectEx is called on new thread for connection purpose only, though still I don't know what is the exact reason of this problem. – summerlight Jun 29 '10 at 08:13

2 Answers2

2

I think you answer your own question with your comment.

Your sequence of events is incorrect, you say that you Bind, ConnectEx, Associate to IOCP.

You should Bind, associate the socket with the IOCP and THEN call ConnectEx.

Len Holgate
  • 21,282
  • 4
  • 45
  • 92
0

Even after you associate your accepted socket to IOCP, your worker thread will remain blocked on GetQueuedCompletionStatus untill you post an "unlocking" completion event. Completion events for receive/write operation wo'nt be sent by the system unless you "unlock" your new socket. For details ckeck the source code of Push Framework http://www.pushframework.com It is a C++ network application framework using IOCP. The "unlocking" trick exists in the "IOCPQueue" class.

charfeddine.ahmed
  • 526
  • 2
  • 8
  • 16
  • 1
    Interesting code... IMHO you're creating that IOCP incorrectly, as the dummy socket that you're creating to create the IOCP isn't created for overlapped use as you're using socket() and not `WSASocket()` with `WSA_FLAG_OVERLAPPED` see docs for http://msdn.microsoft.com/en-us/library/aa363862(VS.85).aspx regarding the "opened for overlapped I/O completion" requirement for the socket. Secondly if all you're using that first socket for is to create the IOCP then you can simply pass `NULL`. Thirdly I've never found that any 'unlocking' is required when using IOCPs as the docs suggest you should. – Len Holgate Jul 20 '10 at 16:17
  • Regarding Len's comment above, according to: http://msdn.microsoft.com/en-us/library/ms740506%28v=vs.85%29.aspx in the Remarks section: "The socket that is created will have the overlapped attribute as a default" – Greg Brown Aug 28 '13 at 17:32