Re: Bind on port fails while switch the user.



On Jun 10, 4:11 pm, Jeffrey Dutky <jeff.du...@xxxxxxxxx> wrote:
On Jun 10, 1:34 pm, "avakharia_ibs...@xxxxxxxxx"

<avakharia_ibs...@xxxxxxxxx> wrote:
Hi All,

I have sent one post in this group long before. Issue was my
application was not able to bind on port.
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 port on
application launch, bind function was always failed.

 Actually 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 port
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 = 1;

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

errout:
close(fd);
errno = 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.
.



Relevant Pages

  • Re: How to listen to more a specific IP and specific ports
    ... I have read this long time ago in the textbook Unix Network Programming ... service and connect our socket to INADDR_ANY and then listen for incoming ... Its clear to me on how to bind with ... port numbers. ...
    (microsoft.public.win32.programmer.networks)
  • Re: How to listen to more a specific IP and specific ports
    ... bind my socket to specific unlimited and selective IP addresses. ... Moreover, in Unix/Linux Berekly network programming, you can determin the ... port numbers. ...
    (microsoft.public.win32.programmer.networks)
  • Re: How to listen to more a specific IP and specific ports
    ... bind my socket to specific unlimited and selective IP addresses. ... port numbers. ... is not feasible because the particular IPs are not fixed. ...
    (microsoft.public.win32.programmer.networks)
  • Re: How to listen to more a specific IP and specific ports
    ... bind my socket to specific unlimited and selective IP addresses. ... Moreover, in Unix/Linux Berekly network programming, you can determin the IP ... port numbers. ...
    (microsoft.public.win32.programmer.networks)
  • BIND 9.5.2rc1 is now available.
    ... BIND 9.5.2rc1 is now available. ... triggering an assertion failure in ... API and glibc hides parts of the IPv6 Advanced Socket ... need to tweak ISC_SOCKET_MAXSOCKETS at compilation time ...
    (comp.protocols.dns.bind)