The close
call only marks the TCP socket closed. It is not usable by process anymore.
But kernel may still hold some resources for a period (TIME_WAIT, 2MLS etc stuff).
Setting of SO_REUSEADDR should remove binding problems.
So be sure that value of true
is really non-zero when calling setsockopt
(overflow bug may overwrite it):
true = 1;
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int))
There is pid
variable is your code. If you use fork
(for starting connection handling processs), then you should close sock
also in the process which does not need it.