Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Mac > Mac Programmer Help > Re: Bind on ***...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 3768 of 3840
Post > Topic >>

Re: Bind on ****t fails while switch the user.

by "avakharia_ibsdev@[EMAIL PROTECTED] " <avakharia_ibsdev@[EMAIL PROTECTED] > Jun 13, 2008 at 10:34 AM

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
 




 4 Posts in Topic:
Bind on port fails while switch the user.
"avakharia_ibsdev@[E  2008-06-10 10:34:17 
Re: Bind on port fails while switch the user.
Jeffrey Dutky <jeff.du  2008-06-10 13:11:56 
Re: Bind on port fails while switch the user.
Jeffrey Dutky <jeff.du  2008-06-10 22:53:38 
Re: Bind on port fails while switch the user.
"avakharia_ibsdev@[E  2008-06-13 10:34:05 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Sep 7 0:51:16 CDT 2008.