On Jun 11, 10:53=A0am, Jeffrey Dutky <jeff.du...@[EMAIL PROTECTED]
> wrote:
> On Jun 10, 4:11=A0pm, Jeffrey Dutky <jeff.du...@[EMAIL PROTECTED]
> wrote:
>
>
>
>
>
> > On Jun 10, 1:34=A0pm, "avakharia_ibs...@[EMAIL PROTECTED]
"
>
> > <avakharia_ibs...@[EMAIL PROTECTED]
> wrote:
> > > Hi All,
>
> > > I have sent one post in this group long before. Issue was my
> > > application was not able to bind on ****t.
> > > Senerio was one client was connected successfully and then I close
the=
> > > application and If I switch the user and try to bind the the ****t on
> > > application launch, bind function was always failed.
>
> > > =A0Actually there is no logic. But with these two changes it is
workin=
g
> > > perfect. If someone Know the reason for it and specific to MAC.
Please=
> > > let me know.
>
> > There IS logic to it and it has nothing to do with your Media Access
> > Controller (or, with your Mac, if that's what you mean by the
> > misplaced acronym).
>
> > Sockets stay bound for a short period of time after they have been
> > closed, up to several seconds. There is a socket option that you can
> > set when binding the socket that will allow you to rebind to a ****t
> > that was recently closed. See "Advanced Programming in the UNIX
> > Environment (2nd Ed.)" by Rago and Stevens, chapter 16 (I think it's
> > in section 16.6, page 579-581) for the gory details.
>
> Just to be absolutely clear, here is the relevant passage from
> APUE(2Ed) on page 580:
>
> =A0 =A0The function in Figure 16.10 fails to operate properly when the
> server
> =A0 =A0terminates and we try to restart it immediately. Normally, the
> implementation
> =A0 =A0of TCP will prevent us from binding the same address until a
> timeout
> =A0 =A0expires, which is usually on the order of several minutes.
Luckily,=
> the
> =A0 =A0SO_REUSEADDR socket option allows us to bypass this restriction,
as=
> =A0 =A0is illustrated in Figure 16.20.
> =A0 =A0---
> =A0 =A0#include "apue.h"
> =A0 =A0#include <errno.h>
> =A0 =A0#include <sys/socket.h>
>
> =A0 =A0int initserver(int type, const struct sockaddr *addr, socklen_t
> alen, int qlen)
> =A0 =A0{
> =A0 =A0 =A0 int fd, err;
> =A0 =A0 =A0 int reuse =3D 1;
>
> =A0 =A0 =A0 if((fd =3D socket(addr->sa_vamily, type, 0)) < 0)
> =A0 =A0 =A0 =A0 =A0return -1;
> =A0 =A0 =A0 if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
sizeof(int=
))
> < 0)
> =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0err =3D errno;
> =A0 =A0 =A0 =A0 =A0goto errout;
> =A0 =A0 =A0 }
> =A0 =A0 =A0 if(type =3D=3D SOCK_STREAM || type =3D=3D SOCK_SEQPACKET)
> =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0if(listen(fd, qlen) < 0)
> =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 err =3D errno;
> =A0 =A0 =A0 =A0 =A0 =A0 goto errout;
> =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 }
> =A0 =A0 =A0 return fd;
>
> =A0 =A0errout:
> =A0 =A0 =A0 close(fd);
> =A0 =A0 =A0 errno =3D err;
> =A0 =A0 =A0 return -1;
> =A0 =A0}
> =A0 =A0----
> =A0 =A0Figure 16.20 =A0Initialize a socket endpoint for use by a server
wi=
th
> address reuse
>
> =A0 =A0To enable the SO_REUSEADDR option, we set an integer to a nonzero
> value and
> =A0 =A0pass the address of the integer as the val argument to
> setsockopt(). We set the len
> =A0 =A0argument to the size of an integer to indicate the size of the
> object to which val points.
>
> If you do that you shouldn't have any further problems with bind
> failing.- Hide quoted text -
>
> - Show quoted text -
Hi Jeffrey,
Thank you for your reply.
I am sorry for using Mac acronym. I meant MAC OSX. I found this issue
with MAC OSX. I am totally agree with your point that socket binds to
that ****t for sometime after we close it. I am surprised that my
application was failing after using SO_REUSEADDR too. You can find my
last post I have posted my code there.
Thanks,
Archita


|