[Gambas-user] Old problem.

Benoit Minisini gambas at ...1...
Wed Nov 19 20:54:36 CET 2003


Le Mercredi 19 Novembre 2003 13:34, danielcampos at ...282... a écrit :
> >>Do the following before binding the socket:
> >>
> >>  int auth = 1;
> >>  setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &auth, sizeof(int));
> >>
> >>I don't know if it works, I read this in a book, and I don't know if it
> >> works on other systems than Linux.
>
> Thanks, I didn't know that option at all, but I'll add it as seems to work
> fine. I've searched more about it at internet, and some people even thinks
> this is a bug  from Linux sockets implementation. This option seems to be
> not standard, some UNIXes support it, some other support it but doesn't
> need it to "rebind" the socket, and some other does not support it... I've
> found that, for example :
>
> "---------------------------
> Now back to the bug in Linux. When you open a socket, you must also call
> the setsockopt() function with the following segment of code:
>
> #ifdef LINUX
>
> opt = 1; len = sizeof(opt);
> setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,&len);
> #endif

setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,len); /* Without the '&' */

>
>
> The #ifdef and #endif statements are necessary only if you want to port the
> code over to systems other than Linux. Some UNIX systems might not support
> or require the SO_REUSEADDR flag.
>
> '-----------------------------------------------------------
>
> So people trying to port GAMBAS to FreeBSD or other systems will need to
> test that socket implementation in their O.S.
>
> Regards,
>
> Daniel Campos
>
>

This is not a bug, this is a Linux feature ! :-)

When you close the socket, the kernel must keep the socket alive because the 
TCP protocol must guarantee the data integrity, and so the kernel must check 
that all pending data are sent and received before effectively destroy the 
socket. 

By default, you can reuse a closed port after about one minute.

Regards,

-- 
Benoit Minisini
mailto:gambas at ...1...





More information about the User mailing list