Home News Winsock Socket Blocking Timeout Solutions

Winsock Socket Blocking Timeout Solutions



Over the past few days, some users reported that Winsock socket blocking timed out.




2.15 – How do I change the time limit for the WinSock function?

Timeout is built into some of the winsock blocking functions (like connect ()). The theory behind this is that only the stack has all the information it needs to set a reasonable time. However, some people think that the value used by the stack has been used for too long. This may take a minute or more.

You can adjust the delays for send () and recv () with the set_optND () parameters for SO_SNDTIMEO and SO_RCVTIMEO. …

For other winsock functions, the best solution is no socket blocking at all. All non-blocking socket methods have options for generating custom timeouts:

winsock blocking socket timeout

  Non-blocking sockets with select () - The fifth parameter to the select () function is the timeout value.Asynchronous Sockets - Use the Windows API SetTimer ().Event Objects - WSAWaitForMultipleEvents () has a timeout parameter.Wait Timers - Call CreateWaitableTimers () to create a serviced timer, which can then be passed using your sockets to a function like WSAEventSelect (): if none of the sapIf no signal is received before the timer expires, the lock function returns regardless of. 

Note that using asynchronous non-blocking sockets can help you avoid timeouts altogether. Your program will continue to run even if Winsock is busy. Thus, you can leave it up to the user to undo an operation that is taking too long, or you can simply leave Winsock’s natural timeout out instead of executing the function in your code.


Three modes of operation of sockets are directly comparable to three modes of communication when trying to get through by phone:

  • Block (wait for the answer of the interlocutor)
    The WinSock function does not return until the operations are complete. This option is suitable for simple applications
  • Non-blocking (hang up and call back)
    The WinSock function returns immediately (failure or success). The client should ask the server to see if the server has finished processing.
  • Asynchronous (leave a message h If the subscriber called you)
    The WinSock DLL sends a message to the client to inform it that the server has finished processing. This is a choice.
  • Interception Blocking Methods

    As stated in Block / NoBlocking and data instability , Windows Sockets implements blockingSteps to Continue Processing Windows MessagesThis may cause the calling application to receive a Windows messageEmbassy. In some situations, the application may want to influence or changehow this pseudo-blocking process is implemented. inWSASetBlockingHook ()offers the ability to replace a named procedure with Windows socketsThe implementation should be used when the processor is shutting down during a “halt”.Operation.