Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 
share|improve this question
    
Is there something already listening on port 8080? What does netstat say? e.g. netstat -a -n -f inet | grep '127.0.0.1:8080' or similar. –  Mikel Feb 6 '11 at 21:29
    
Mikel: that command produces no results. Running just netstat -a -n -f inet also produces nothing interesting: just some tcp connections to stackoverflow and a bunch of udp ports. –  Noah Lavine Feb 6 '11 at 21:34
    
Oh, and running Guile with root privileges doesn't fix it. –  Noah Lavine Feb 6 '11 at 21:45
add comment

1 Answer 1

up vote 2 down vote accepted

This isn't exactly an answer, since it doens't directly solve you problem...but it looks as if your problem is particular to guile. That is, on my OS X 10.6 system, I have the same problem you do using the guile code from you example. On the other hand, the following works just fine:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('127.0.0.1', 8080))
s.listen(5)

c, addr = s.accept()
c.send('hello\n')
c.close()

If I run this code, I can see the socket listening:

netstat -f inet -an | grep 8080
tcp4       0      0  127.0.0.1.8080         *.*                    LISTEN

And connecting to port 8080 does what you would expect:

$ telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello
Connection closed by foreign host.
share|improve this answer
    
Yep, I just replicated your results. It must be a problem with Guile. I'll talk to the development list and track it down. –  Noah Lavine Feb 7 '11 at 0:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.