Why can't a bind linux service to the loop-back only?

Posted by Jon Trauntvein on Stack Overflow See other posts from Stack Overflow or by Jon Trauntvein
Published on 2011-01-07T22:24:42Z Indexed on 2011/01/08 0:54 UTC
Read the original article Hit count: 185

Filed under:
|
|
|
|

I am writing a server application that will provide a service on an ephemeral port that I only want accessible on the loopback interface. In order to do this, I am writing code like the following:

struct sockaddr_in bind_addr;

memset(&bind_addr,0,sizeof(bind_addr));
bind_addr.sin_family = AF_INET;
bind_addr.sin_port = 0;
bind_addr.sin_addr.s_addr = htonl(inet_addr("127.0.0.1"));
rcd = ::bind(
   socket_handle,
   reinterpret_cast<struct sockaddr *>(&bind_addr),
   sizeof(bind_addr));

The return value for this call to bind() is -1 and the value of errno is 99 (Cannot assign requested address). Is this failing because inet_addr() already returns its result in network order or is there some other reason?

© Stack Overflow or respective owner

Related posts about c

    Related posts about linux