Friday, March 10, 2006
Socket, reuse the same port
Use case:
After server is crashed, and is started over again, it will try to listen on the pre-configured port.
During a period of time(2 minutes or so), the port status is CLOSE_WAIT. If the port is not reusable, then the second binding will fail.
Solution
Use SO_REUSEADDR option for the socket,
For example, in gSoap,
soap.bind_flags = SO_REUSEADDR;
master = soap_bind(&soap, NULL, port, 100);
in socket programming, we can use setsockopt, please refer to
http://www.unet.univie.ac.at/aix/libs/commtrf2/setsockopt.htm