-
Address already in use
Hi All,
Got a problem restarting my server:
bind: Address already in use
I start it like:
Code:
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)
{
perror("setsockopt(SO_REUSEADDR)");
close(s);
return 3;
}
if(bind(s,(struct sockaddr *)&srv,sizeof(srv)) == -1)
{
perror("bind");
close(s);
return 4;
}
if(listen(s,3) == -1)
{
perror("listen");
close(s);
return 5;
}
So it should be reusing the socket.
Had a look in nestat and:
Code:
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
Connections in CLOSE_WAIT state.
Any ideas how I can get round this?
Cheers, rotis23
-
so_resuse should do the trick.
Do you have something like
memset(&(srv.sin_zero), '\0', 8);
I presume sock_opt is an int?
-
Hi Hammer,
When you say so_resuse, you are agreeing with SO_REUSEADDR?
Hmm. I don't memset anything, only this before previously posted code:
Code:
int s, sock_opt, client, cli_size, pid, res, result = 0;
struct sockaddr_in srv, cli;
//fork code
//signal handler code
s = socket(AF_INET, SOCK_STREAM, 0);
if(s == -1)
{
perror("socket");
return 2;
}
Should I be more careful here?
-
>>When you say so_resuse, you are agreeing with SO_REUSEADDR?
Yes
>>memset
Yes, you should be memset()'ing.
Sample:
http://personal.windsofstorm.net/tserver.c
-
-
I had a look around after your last post and noticed that there are:
SO_REUSEADDR
SO_REUSEPORT
From this thread I guess SO_REUSEPORT is not needed because it allows multiple processes to bind to the same address:
http://www.unixguide.net/network/socketfaq/4.11.shtml
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.