Why can't I bind to 127.0.0.1 on Mac OS X?
- by Noah Lavine
Hello,
I'm trying to set up a simple web server on Mac OS X, and I keep getting an error when I run bind. Here's what I'm running (this transcript uses GNU Guile, but just as a convenient interface to posix).
(define addr (inet-aton "127.0.0.1")) ; get internal representation of 127.0.0.1
(define sockaddr (make-socket-address AF_INET addr 8080)) ; make a struct sockaddr
(define sock (socket PF_INET SOCK_STREAM 0)) ; make a socket
(bind sock sockaddr) ; bind the socket to the address
That gives me the error In procedure bind: can't assign requested address.
So I tried it again allowing any address.
(define anyaddr (make-socket-address AF_INET INADDR_ANY 8080)) ; allow any address
(bind sock anyaddr)
And that works fine. But it's weird, because ifconfig lo0 says
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
inet 127.0.0.1 netmask 0xff000000
So the loopback device is assigned to 127.0.0.1. So my question is, why can't I bind to that address?
Thanks.
Update: the output of route get 127.0.0.1 is
route to: localhost
destination: localhost
interface: lo0
flags: <UP,HOST,DONE,LOCAL>
recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire
49152 49152 0 0 0 0 16384 0