Hi Marty,
The sockopt REUSEADDR is intended to allow the local address to be
reused when the server is restarted before the required wait time
expires.
That's true. But there are other things as well. SO_REUSEADDR allows a
socket to bind to a port that another socket is bound to, thus
eliminating the need to wait for the close-wait timeout.
However, it does NOT allow two sockets to LISTEN to the same port. The
previous listener must be closed.
I thought that perhaps my SO_LINGER option was conflicting with
SO_REUSEADDR, so I removed SO_LINGER.
SO_LINGER controls how long the socket waits on the close() API. When
linger is turned on, the close() API will wait for any unsent data to be
sent. Assuming your sockets are in blocking mode, your program will sit
and wait on the close() API until any data in the socket's "send buffer"
has been sent to the remote host, and won't actually close until that
data is sent -- or until the timeout value (the l_linger value) has
elapsed. IF that timeout is elapsed, the remaining data will be discarded.
You have yours set to 1 second, which means that close() can wait for up
to 1 second, and if there's still data in the send buffer, it'll be
discarded.
The normal behavior (when l_onoff = 0, i.e. the default), is for close()
to return immediately, and let i5/OS continue to attempt to send the
"send buffer" data in the background. It will continue to try for as
long as the timeout value set with the TCPCLOTIMO parameter of the
CHGTCPA command.
However, I can't see how this could cause bind() to fail. Even with the
default wait time of 120 seconds, if you've set SO_REUSEADDR correctly,
you're able to re-start the server program without problems.
I would suspect that either your listener socket (the one you called the
listen() API on) has not been closed properly, or that (for some unknown
reason?) the setsockopt() API for SO_REUSEADDR is failing.
Do you check whether setsockopt() was successful? If it fails, have you
checked the value of errno()?
Did you make sure that you closed the socket that you called listen()
on? Did the close() complete without error? Remember, your program
has two sockets... one that you create with the socket() API, and
another that you create by calling the accept() API. Both should be
closed. (But, it's the former of the two that will cause the error
message you posted.)
hope that helps...?
As an Amazon Associate we earn from qualifying purchases.