const int reuse = 1;
setsockopt (handle, SOL_SOCKET, SO_REUSEADDR, (const char*) &reuse, sizeof (reuse));
On MAC all is behaving as I need it and also the behaviour I was infering from the JUCE docu.
Making the SO_REUSEADDR socket option "mac only" gives me the desired behaviour on windos too.
The server is part of a VST plugin. I run it in reaper.
On OSX I noticed that the port is still open, with "reaper" as proccess, when I unload my plugins (the plugin & JUCE gets properly unloaded, all destructores get called etc., ).
I check the ports with:
sudo lsof -nP -iTCP -sTCP:LISTEN
The socket port only gets freed after I close REAPER, so one needs the SO_REUSEADDR on MAC to be able to reopen the port. Strangely enought I can not open the same port with a second plugin (the second node). although this is what i desire, i would no expect it from what i read in the c++ docs.
So theoreticly the option should be set to 0 on both systems but on MAC it is needed as the socket stays in some kind of bound state.
On Windows on the other hand the default (eg no options set) bahaviour seems similar to the MAC behaviour WITH the SO_REUSEADDR option set. It seems on windows the option will enable other servers to "steal" the port (which is actually what i would expect when reading the docs, ass opposed to the MAC/LINUX behaviour), thus I can always bind to it and never will get a false in my very first code example. I tried SO_EXCLUSIVEADDRUSE, which is supposed to stop the port/adress stealing, thought it did not work. I did not try SO_REUSEPORT.
I suggest to just use it only on MAC (possibly LINUX builds, as its supposed to behave the same) and remove the option from Windows builds. The port is not supposed to be "stolen", at least this is how i read the juce doc for:
bool InterprocessConnectionServer::beginWaitingForSocket (int portNumber);
and also how I need it right new :P :)