2

I am testing a Socket class I have written that wraps the Linux socket API.

In a test function I do the following:

  1. Create a server socket
  2. bind/listen
  3. connect from a new socket
  4. accept on a new socket
  5. send some data between them with recv and send.
  6. call shutdown(SHUT_WR) on both sides
  7. call recv on both sides, and get a 0 return value
  8. call close on all three sockets

I then immediately repeat these 8 steps again.

I find that intermittantly on the bind in the second iteration of the steps I get EADDRINUSE.

I was of the impression that it was possible to perform an orderly shutdown in such a fashion that this was avoidable. I am aware of SO_REUSEADDR - but is it possible to do an orderly shutdown and block until the port is available again?

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • 2
    You could sleep for 2MSL which is like 2minutes (cat /proc/sys/net/ipv4/tcp_fin_timeout ) on my Linux box to make sure that no old stray packets will screw up your new server instance, but if you're testing locally, it's AFAIK, totally pointless because you won't have any stray packets and the simplest thing to do would be to set SO_REUSEADDR. Check out http://www.serverframework.com/asynchronousevents/2011/01/time-wait-and-its-design-implications-for-protocols-and-scalable-servers.html or http://stackoverflow.com/questions/3229860/what-is-the-meaning-of-so-reuseaddr-setsockopt-option-linux – Petr Skocik Aug 23 '16 at 22:42
  • C does not support classes. What's wrong with the POSIX standard library? – too honest for this site Aug 23 '16 at 23:00
  • This might be helpful for you: http://stackoverflow.com/questions/3757289/tcp-option-so-linger-zero-when-its-required. – Ton Plooij Aug 24 '16 at 07:29

0 Answers0