03-16-06 12:51 PM
Even when using the getsockopt() function with SO_REUSEADDR
socketoption, i still get the message: Error: connections.c:
conn_listen: bind (Address already in use)
How can i fix this problem?
a part of the code:
/* create us a socket.. */
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
/* ..and set it to non-blocking */
if (-1 == (flags = fcntl(s, F_GETFL, 0))) flags = 0;
if (-1 == (fcntl(s, F_SETFL, flags | O_NONBLOCK)))
{
log(LOG_ERROR, "connections.c: conn_listen: fcntl");
return NULL;
}
bzero(&sin, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
/* If there is some old socket crap hanging in kernel, this
avoid the
"Address already in use" error while using bind()
But u also gotta be careful not to use conn_listen on a
socket used somewhere else ;-p */
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(int)) == -1) {
log(LOG_ERROR, "connections.c: conn_listen:
setsockopt");
return NULL;
}
if (bind(s, (struct sockaddr *)&sin, sizeof(struct
sockaddr_in)) == -1)
{
log(LOG_ERROR, "connections.c: conn_listen: bind");
return NULL;
}
netstat output:
Proto Recv-Q Send-Q Local Address Foreign Address
State
tcp 0 0 *:22000 *:*
LISTEN
[ Post a follow-up to this message ]
|