Address already in use
Got a problem restarting my server:
bind: Address already in use
I start it like:
So it should be reusing the socket.
srv.sin_addr.s_addr = INADDR_ANY;
srv.sin_port = htons( port);
srv.sin_family = AF_INET;
sock_opt = 1;
if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&sock_opt,sizeof(sock_opt)) == -1)
if(bind(s,(struct sockaddr *)&srv,sizeof(srv)) == -1)
if(listen(s,3) == -1)
Had a look in nestat and:
Connections in CLOSE_WAIT state.
tcp 25 0 localhost:5580 localhost:40353 CLOSE_WAIT
tcp 48 0 localhost:5580 localhost:40352 CLOSE_WAIT
tcp 25 0 localhost:5580 localhost:40361 CLOSE_WAIT
tcp 48 0 localhost:5580 localhost:40360 CLOSE_WAIT
Any ideas how I can get round this?
so_resuse should do the trick.
Do you have something like
memset(&(srv.sin_zero), '\0', 8);
I presume sock_opt is an int?
When you say so_resuse, you are agreeing with SO_REUSEADDR?
Hmm. I don't memset anything, only this before previously posted code:
Should I be more careful here?
int s, sock_opt, client, cli_size, pid, res, result = 0;
struct sockaddr_in srv, cli;
//signal handler code
s = socket(AF_INET, SOCK_STREAM, 0);
if(s == -1)
>>When you say so_resuse, you are agreeing with SO_REUSEADDR?
Yes, you should be memset()'ing.
I had a look around after your last post and noticed that there are:
From this thread I guess SO_REUSEPORT is not needed because it allows multiple processes to bind to the same address:
I'm also not sure if this is implemented on RedHat Linux systems or if it's just BSD.
So, are you fixed or not?
Not sure - going to do some load testing tomorrow and see if I can reproduce.
I've added the memset though.