Winsock 2 - what error to expect from accept()

kirillandy 81 Reputation points
2021-09-30T16:00:40.497+00:00

Hello! I have a small question regarding the behavior of the accept() function in Winsock2. It's a bit of an awkward one, which I could probably figure out myself, but I can't get my networking equipment to cooperate and reproduce the test case :D. I was hoping someone had to handle this possible error at some point and could let me know what to expect (or not expect if this a wild goose chase).

The question is simple: so suppose I have an AF_INET TCP server socket bound to an address and listening. I make a blocking accept() call and wait for connections. What would happen in case of an event that makes the blocking socket... um... "invalid"? Like a disconnect from the network, or a change in IP address?

I suspect that there should be a scenario where accept() returns with an error, because a socket tied to, say, a Wi-Fi interface W in network MyNetwork, with a dynamic IP address of N.N.N.10, let's say, simply CAN'T accept anything after we disconnect from the network, or, for whatever earthly reason, get re-assigned address N.N.N.(not 10)?

I did do some basic checking.

1) Manually disconnecting from a network while you've got a blocking socket gives... nothing. The socket keeps blocking and, moreover, re-connecting to the network and attempting to receive stuff actually continues to work! So simple disconnects don't do anything.

2) I tried to force my PC's IP address available in my router settings (there's a big button with "Force available" written on it :D), but the blocking socket still received stuff. Not sure if this is indicative of anything, since the DHCP lease time probably still needs to run out after the "force available", I haven't got any documentation that details how the feature works...

3) I tried disconnecting and connecting back after the "force available". Same thing, connections can still come through. I am assigned the same old IP address, so it doesn't seem any different to scenario (1)

I could just disable my Wi-Fi interface while I'm at it, at this point, see if that results in anything :D

The question is: what has to happen for a blocking server socket to return with an error that says "mate, you're not in the network any more, there's nothing for me to expect"?
And what error would I get? The main ones I would expect would be WSAEINVAL and WSANETDOWN...

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,387 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 11,081 Reputation points Microsoft Vendor
    2021-10-01T02:41:16.85+00:00

    According to WSAAccept,

    When issuing a blocking Winsock call such as WSAAccept, Winsock may need to wait for a network event before the call can complete. Winsock performs an alertable wait in this situation,.......

    Perhaps what you need is Receiving Connection Notifications.


0 additional answers

Sort by: Most helpful