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 3 of 4 Topic 3768 of 3882
Post > Topic >>

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

by Jeffrey Dutky <jeff.dutky@[EMAIL PROTECTED] > Jun 10, 2008 at 10:53 PM

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
working
> > 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:

   The function in Figure 16.10 fails to operate properly when the
server
   terminates and we try to restart it immediately. Normally, the
implementation
   of TCP will prevent us from binding the same address until a
timeout
   expires, which is usually on the order of several minutes. Luckily,
the
   SO_REUSEADDR socket option allows us to bypass this restriction, as
   is illustrated in Figure 16.20.
   ---
   #include "apue.h"
   #include <errno.h>
   #include <sys/socket.h>

   int initserver(int type, const struct sockaddr *addr, socklen_t
alen, int qlen)
   {
      int fd, err;
      int reuse =3D 1;

      if((fd =3D socket(addr->sa_vamily, type, 0)) < 0)
         return -1;
      if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int))
< 0)
      {
         err =3D errno;
         goto errout;
      }
      if(type =3D=3D SOCK_STREAM || type =3D=3D SOCK_SEQPACKET)
      {
         if(listen(fd, qlen) < 0)
         {
            err =3D errno;
            goto errout;
         }
      }
      return fd;

   errout:
      close(fd);
      errno =3D err;
      return -1;
   }
   ----
   Figure 16.20  Initialize a socket endpoint for use by a server with
address reuse

   To enable the SO_REUSEADDR option, we set an integer to a nonzero
value and
   pass the address of the integer as the val argument to
setsockopt(). We set the len
   argument 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.
 




 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 Fri Dec 5 10:38:03 CST 2008.