-2

I have a service that binds to 127.0.0.1 on a random port. As a part of testing procedure I set up a loopback alias on 192.168.22.2. I can confirm that the alias is visible by ifconfig and that I can ping it.

Now when I curl 127.0.0.1:{server-port} I get a proper response from a server. However when I curl 192.168.22.2:{server-port}. I get an error curl: (52) Empty reply from server. And jetty server throws an exception: java.io.IOException: Socket is not connected.

I'm running MacOS Monterey Version 12.6 (21G115) on M1 Pro and openjdk version "14.0.2".

Below I provide a code sample that fails every single time for me.

https://github.com/codewise/echo-server-loopback-alias

1 Answers1

0

that binds to 127.0.0.1 on a random port

If you configure to bind to a specific IP then only that IP is listening.

That being said, confirm that there is a bound java process listening on the network adapter addresses.

Use a tool like netstat to list the LISTENING socket (ip address + port). Don't forget to turn on the PID for each listening socket (in netstat that would be the --programs command line)

Example:

$ netstat --tcp --numeric --listening --programs
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:33067         0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:3000          0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -                   
tcp6       0      0 127.0.0.1:63342         :::*                    LISTEN      963265/java         
tcp6       0      0 :::38939                :::*                    LISTEN      963265/java         
tcp6       0      0 127.0.0.1:57036         :::*                    LISTEN      2207436/java        
tcp6       0      0 ::1:631                 :::*                    LISTEN      -                   
tcp6       0      0 127.0.0.1:33215         :::*                    LISTEN      963265/java         
tcp6       0      0 127.0.0.1:34351         :::*                    LISTEN      1497959/java        
tcp6       0      0 127.0.0.1:40785         :::*                    LISTEN      2207436/java        
tcp6       0      0 127.0.0.1:20432         :::*                    LISTEN      1497959/java        
tcp6       0      0 :::80                   :::*                    LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
tcp6       0      0 127.0.0.1:6942          :::*                    LISTEN      963265/java         

Now, back to your specific example ...

sudo ifconfig lo0 alias 192.168.22.2

You are attempting to add a 192.* IP (not publicly routeable, private address space) which is a not a loopback address space, as an IP alias to a loopback device, yeah. That's not going to work. Use a different loopback address instead (eg: 127.0.0.22) for your lo0 adapter.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • The test `EchoServer` listens on `*:51296` as listed by `lsof -i -P | grep LISTEN`. You mentioned that the loopback should not work by design but the very same code works for other developers on my team which is why I turned to SO. – Rafał Rak Oct 24 '22 at 14:30