user12211419
user12211419

Reputation:

listen tcp 127.0.4.1:2040: bind: can't assign requested address

I am using this golang code:

l, err := net.Listen("tcp", "127.0.4.1:2040")

if err != nil {
    log.Fatal("d0b9184a-5248-413e-a5a8-30fea66997f5:", err)
}

log.Fatal(s.Serve(l))

I am getting this error:

listen tcp 127.0.4.1:2040: bind: can't assign requested address

anyone know why that won't work?

Upvotes: 9

Views: 8949

Answers (1)

shmsr
shmsr

Reputation: 4204

You have to add an additional IPv4 address to the loopback interface:

sudo ifconfig lo0 alias 127.0.4.1

Before running the command you can verify the routes using:

netstat -nr

By default (On MacOS Catalina 10.15.4), these routes are present:

127                127.0.0.1          UCS            lo0
127.0.0.1          127.0.0.1          UH             lo0

After you have added your desired address to the loopback interface (i.e, lo0), netstat -nr would show:

127                127.0.0.1          UCS            lo0
127.0.0.1          127.0.0.1          UH             lo0
127.0.4.1          127.0.4.1          UH             lo0

Now try using your program, it should work.

Also, this is temporary. It won't persist on reboot. For that give this a read!

Upvotes: 7

Related Questions