Winsock takes time out

Is it possible to set the timeout of the function to accept when using blocking winsockets? How can we do to return and send functions through setsockopt?

This seems impossible, but I want to provide.

+4
source share
2 answers

The selection function can be used with a timeout. Although selection is most often used with non-blocking sockets, I have not read anything that prevents the blocking socket from being passed for selection. If the select function is disabled, then you have an equivalent behavior equal to the receive timeout.

From MSDN

"The readfds parameter defines the sockets that should be checked for readability. If the socket is currently in the listening state, it will be marked as readable if an incoming request has been received, so accept is guaranteed to terminate without blocking . For other sockets, readability means that the data in the queue is readable, so calling recv, WSARecv, WSARecvFrom, or recvfrom is not guaranteed to block. "

This should give you the behavior you expect.

+7
source

It’s quite easy to organize another thread to close the listening jack and set a timeout. Create an auto-reset event and pass it whenever accept () returns. Write a function that waits for an event with WaitForSingleObject () in a loop with a timeout interval. If WFSO returns with anything that is not WAIT_OBJECT_0, close the listening socket and exit it. Before you enter the accept () loop, create a thread to call the function.

Another possibility is to use acceptEx () and wait with the WFSO () timeout for the event passed as the hEvent of the OVERLAPPED structure. If the timeout is triggered, use CancelIo () to remove the completed completion of acceptEx () from the IO network.

0
source

Source: https://habr.com/ru/post/1401156/


All Articles