An asynchronous socket error on Windows occurs when a program using non-blocking sockets (like Winsock’s WSAAsyncSelect or overlapped I/O) fails during an operation, often surfacing as error 10035 (WSAEWOULDBLOCK), 10060 (connection timed out), or 10061 (connection refused). To fix it, you need to identify the specific error code, check your firewall/antivirus, verify the server is reachable, and ensure your code properly handles the asynchronous pattern. This article explains the common causes, how to diagnose them, and step-by-step fixes for each scenario.
What Is an Asynchronous Socket Error?
Windows sockets (Winsock) allow programs to communicate over a network. In asynchronous mode, the program doesn’t wait for an operation (like connecting or sending data) to finish; instead, it continues running and gets notified later via a message or callback. This improves responsiveness but introduces a class of errors where the operation hasn’t completed yet—or failed in the background. The error code you see (e.g., 10035, 10060) tells you exactly what went wrong, but the context matters: a timeout is different from a refused connection, and both require different fixes.
Common Error Codes and What They Mean
- 10035 (WSAEWOULDBLOCK): The socket is marked non-blocking, and the requested operation would block. This is often a normal part of async programming—you should retry or wait for the next notification.
- 10060 (WSAETIMEDOUT): A connection attempt timed out. The server didn’t respond within the timeout period. This usually indicates a network issue, firewall drop, or the server is down.
- 10061 (WSAECONNREFUSED): The server actively refused the connection. This means the server is reachable but no application is listening on that port, or the port is blocked.
- 10054 (WSAECONNRESET): The remote host reset the connection. This can happen if the server crashed, or a firewall sent a RST packet.
- 10048 (WSAEADDRINUSE): The local address/port is already in use. This often occurs when a previous socket wasn’t closed properly.
Why Do Asynchronous Errors Happen?
Asynchronous sockets are used in GUI applications (like chat clients or games) to avoid freezing the interface. The Windows message loop (or a thread pool) processes network events. Errors can occur for several reasons:
- Incorrect handling of WSAEWOULDBLOCK: Many programmers treat this as a fatal error, but it’s a signal to wait for the next FD_WRITE or FD_READ notification.
- Firewall or antivirus interference: Windows Defender Firewall or third-party security software may silently block connections, causing timeouts or resets.
- Network instability: Wi-Fi dropouts, VPN issues, or router problems can cause timeouts (10060) or resets (10054).
- Server not listening: If the server application isn’t running or the port is wrong, you’ll get 10061.
- Resource leaks: Not closing sockets properly can lead to 10048 errors over time.
How to Diagnose the Exact Error
Before fixing, you need the exact error code and the operation that failed (connect, send, receive, accept). Follow these steps:
- Look at the error code in your application’s log or debug output. If it’s a Windows Forms app, check the
ErrorCodeproperty of theSocketException. - Check if the error is transient (10035) or persistent (10060/10061). Transient errors may not need a fix—just proper handling.
- Test the server from the same machine using a simple tool like
telnet server_ip portor PowerShell’sTest-NetConnection server_ip -Port port. This tells you if the port is reachable. - If the port is unreachable, check the firewall rules on both the client and server. On Windows, run
wf.mscto open Windows Defender Firewall with Advanced Security and look for blocking rules. - If the port is reachable but you still get 10060, the issue may be in your code’s timeout settings or the server’s backlog queue.
Step-by-Step Fixes for Each Scenario
Fix for 10035 (WSAEWOULDBLOCK)
This is not a real error—it’s a normal part of non-blocking sockets. When you call Send() or Receive() on a non-blocking socket and the operation can’t complete immediately, Winsock returns 10035. The correct handling is to wait for the next FD_WRITE or FD_READ notification and then retry. If you’re using WSAAsyncSelect, ensure you handle the FD_WRITE and FD_READ messages in your window procedure. If you’re using overlapped I/O, use WSAGetOverlappedResult to check the status. Never treat 10035 as a fatal error; it just means “try again later.”
Fix for 10060 (Timeout)
- Check the server: Is it running? Is it listening on the correct port? Use
netstat -an | findstr :portto verify. - Check firewall: Temporarily disable Windows Defender Firewall (or the specific rule) to test. If it works, add an inbound rule for the port.
- Check network: Ping the server’s IP. If ping fails, the server is unreachable—check cables, Wi-Fi, or VPN.
- Increase timeout: In your code, if you set a timeout for the connection (e.g.,
ConnectAsyncwith a timeout), increase it. Some networks are slow. - Check for IP/port mismatch: Ensure the server IP and port are correct. A common mistake is using the wrong port or a localhost address when the server is remote.
Fix for 10061 (Connection Refused)
- Verify the server is listening: Run
netstat -an | findstr :porton the server. If nothing shows, the server application isn’t bound to that port. - Check the port number: Ensure the client is connecting to the correct port. A typo (e.g., 8080 vs 80) will cause this.
- Check if the server is bound to a specific IP: If the server binds to 127.0.0.1 only, it won’t accept connections from other machines. Change it to 0.0.0.0.
- Firewall may be blocking with RST: Some firewalls send a TCP RST instead of silently dropping. Temporarily disable the firewall to test.
Fix for 10054 (Connection Reset)
- This often indicates the server crashed or closed the connection abruptly. Check server logs.
- If you’re the server, ensure you handle client disconnects gracefully—don’t call
Close()on a socket that’s still sending data. - If you’re the client, retry the connection after a short delay.
Preventing Asynchronous Socket Errors
Prevention is better than debugging. Here are best practices:
- Always handle WSAEWOULDBLOCK: Write a helper function that retries the operation when this error occurs.
- Set reasonable timeouts: Use
SO_RCVTIMEOandSO_SNDTIMEOfor blocking sockets, or implement your own timeout for async operations. - Close sockets properly: Always call
Shutdown()followed byClose()to release resources. - Use try-catch around socket calls: Catch
SocketExceptionand check theErrorCodeto handle specific errors. - Test on both localhost and remote: Many errors only appear when connecting to a different machine due to firewall or network differences.
Asynchronous vs. Synchronous Sockets: Which to Use?
If you’re getting frequent async errors, you might wonder if synchronous sockets are better. Synchronous sockets block the calling thread, which can freeze the UI if done on the main thread. Asynchronous sockets are necessary for responsive applications, but they require careful error handling. For simple tools or scripts, synchronous sockets are easier to debug. For production GUI apps, use async with proper error handling. You can also use async/await in C# with SocketAsyncEventArgs or NetworkStream to simplify the code while keeping responsiveness.
Frequently Asked Questions
Why do I get error 10035 even though I’m using async?
10035 is expected in non-blocking mode. It means the operation would block if it were synchronous. Your code should treat it as “retry later” and wait for the next event notification. If you get it repeatedly, you may be calling Receive() before the FD_READ notification arrives—use the notification to trigger the read.
Can a firewall cause error 10060 or 10061?
Yes. A firewall can silently drop packets (causing 10060) or send a TCP RST (causing 10061). Temporarily disable the firewall to test. If the error disappears, add an inbound rule for the port. On Windows, use Windows Defender Firewall with Advanced Security to create a rule for the specific program or port.
How do I get the error code in C#?
In C#, catch SocketException and read the ErrorCode property. For example: catch (SocketException ex) { int code = ex.ErrorCode; }. The code is the Winsock error number (e.g., 10060). You can also use ex.SocketErrorCode for a friendlier enum.
Is error 10035 a sign of a network problem?
No, it’s a normal condition in non-blocking sockets. It indicates that the socket’s send or receive buffer is temporarily full or empty. It’s not a network failure. If you see it constantly, it may mean your application is not processing notifications fast enough, but it’s not a network issue.
What does “connection reset” mean?
Error 10054 means the remote host closed the connection abruptly, often by sending a TCP RST packet. This can happen if the server crashed, if there’s a firewall that sends RST, or if the server’s application closed the socket while data was still in transit. Check server logs and ensure graceful shutdown.
Should I use WSAAsyncSelect or overlapped I/O?
WSAAsyncSelect is older and ties socket events to a window message loop, which is fine for GUI apps. Overlapped I/O is more flexible and works well with thread pools, but it’s more complex. For modern C# applications, consider using SocketAsyncEventArgs or async/await with NetworkStream, which handle the complexity for you.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Gmail Gemini Privacy: Data Access & Limit Settings
- How to Set Up Apple Pay on iPhone
- How to Choose a Refurbished iPhone With Years of Support
- Fix Outlook Inbox Corruption with DISM in Windows
- Voice Dictation in Google Docs: How to Use It
- Turn Off Message Notifications on Mac