0

I recently read through the Socket.BeginAccept() - MSDN article to find out, how to properly cancel this operation if it, for example, has timed out. This article says, I need to call Close() on the socket to cancel this operation, but when I call this, I cannot accept other Connections anymore. I want to cancel only the Accept-Operation which timed out. I do not want to close the whole socket to cancel all andalso future BeginAccept() calls. Is there another way to cancel a single Accept operation?

Best regards

johmarjac
  • 57
  • 6
  • The article you read wasn't lying. You need to close the socket to cancel the accept. That's just how sockets work. Interrupting pending operations on a socket always leaves the socket in an unusable state. Frankly, it's doubtful you need to cancel the accept operation at all; that's something you do if you're shutting the server down, in which case closing the socket is fine. If you want to allow future accept operations, then you just leave the socket in the accept operation. – Peter Duniho Jan 13 '18 at 23:12
  • 1
    The question isn't clear. **Why** do you want to cancel your `BeginAccept` call without closing the socket? Please note that here on SO we frequently get questions along the line of "I am trying to shoot off my foot without making it impossible to stand. How can I do it?" Invariably we need to get to the bottom of why you're trying to shoot off your foot to begin with, because most often this is an X/Y problem. You had a problem, for which you thought X was the solution. When you tried to implement solution X you had problem Y, so you go on Stack Overflow to ask how to solve Y. – Lasse V. Karlsen Jan 13 '18 at 23:13

0 Answers0