Search code examples
c++csocketsnetwork-programmingtcpsocket

Using timeouts on socket send() and recv()


Is it a good idea to ALWAYS use either select() or poll() and impose say a 10 second timeout on both send() and recv() calls? Or should I just allow them to block indefinitely?

Does imposing this sort of timeout (using select() or poll()) lead me to lose some sort of error reporting or functionality (in the form of return values) that I can get when just using send() and recv()?

NOTE : Assuming I poll in the same thread right before I call recv() or send(). And the call to poll() is statically well formatted, i.e. The parameters dont change dynamically besides how they do based on the invocation of the wrapped recv() or send()

ANOTHER NOTE : If there is a timeout, then I will throw an exception and let the programmer catch it. I was hoping this would lead to protection against a DOS attack. If there is indeed a timeout. I will throw an exception and not get to calling either recv() or send()

YET ANOTHER NOTE : Source code related to what I am talking about can be found here https://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#faq under a similar FAQ

Thanks!


Solution

  • What are you going to do after timeout lapses? If you are going to start waiting again, than timeout gives you nothing.

    If you will close the connection and brand it dead, than timeout is very useful.