kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Web Framework open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Web Framework

v14.0.0-next.14

Release v1.7.7

v6.0.7

Provide async closures in XCTVapor

angular

v14.0.0-next.14

flask

gin

Release v1.7.7

symfony

v6.0.7

vapor

Provide async closures in XCTVapor

Popular Libraries in Web Framework

Trending New libraries in Web Framework

Top Authors in Web Framework

1

112 Libraries

174057

2

78 Libraries

3474

3

69 Libraries

26046

4

62 Libraries

3090

5

47 Libraries

1701

6

41 Libraries

176

7

40 Libraries

866

8

36 Libraries

1192

9

31 Libraries

24338

10

29 Libraries

2678

1

112 Libraries

174057

2

78 Libraries

3474

3

69 Libraries

26046

4

62 Libraries

3090

5

47 Libraries

1701

6

41 Libraries

176

7

40 Libraries

866

8

36 Libraries

1192

9

31 Libraries

24338

10

29 Libraries

2678

Trending Kits in Web Framework

No Trending Kits are available at this moment for Web Framework

Trending Discussions on Web Framework

    Why can two Java processes bind to the same socket in macOS?
    ImportError: Couldn't import Django inside virtual environment with poetry?
    ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)
    Newbe on Go - AppEngine - Deploy
    Remix: middleware pattern to run code before loader on every request?
    Quart framework WARNING:asyncio:Executing
    dial tcp 127.0.0.1:8080: connect: connection refused. go docker app
    VS2017 crashes with 'FileNotFoundEx: System.Runtime.CompilerServices.Unsafe, V=4.0.4.1' upon loading any project
    Logging access to Java servlet
    DataTables warning: table id=<my_table_name> - Requested unknown parameter '<my_table_first_column_name>' for row 0, column 0

QUESTION

Why can two Java processes bind to the same socket in macOS?

Asked 2022-Feb-16 at 20:18

I have some Java code that is generating a socket binding. It's hard to provide a minimal example as this is part of a web framework, but it effectively does this check at some point.

1  private static boolean portInUse(int port) {
2    // try to bind to this port, if it succeeds the port is not in use
3    try (ServerSocket socket = new ServerSocket(port)) {
4      socket.setReuseAddress(true);
5      return false;
6    } catch (IOException e) {
7      return true;
8    }
9  }
10

I can see that if I run two distinct Java processes with the same port, they both fall into the first conditional and return false, thus both are able to bind to the same port. I've read through some related socket questions and explanations like this one, but they seem to make it sound like this shouldn't be possible with the options I've specified. Looking at the implementation of setReuseAddress it only seems to set SO_REUSEADDR on the socket.

I can see one process ends up with a socket like ServerSocket[addr=0.0.0.0/0.0.0.0,localport=56674] in a debugger. If I run something like sudo lsof -n -i | grep -e LISTEN -e ESTABLISHED | grep 56674 I can see two processes binding to the same port:

1  private static boolean portInUse(int port) {
2    // try to bind to this port, if it succeeds the port is not in use
3    try (ServerSocket socket = new ServerSocket(port)) {
4      socket.setReuseAddress(true);
5      return false;
6    } catch (IOException e) {
7      return true;
8    }
9  }
10java      68863    natdempk 1256u  IPv4 0xbbac93fff9a6e677      0t0  TCP *:56674 (LISTEN)
11java      68998    natdempk  985u  IPv6 0xbbac93fff2f84daf      0t0  TCP *:56674 (LISTEN)
12

I can also see some other projects like gRPC and Node mention this behavior as being observed with their servers in issue trackers, but they never explain why this is possible. How can distinct processes bind to the same socket on macOS?

I am running macOS 11.6.3 (20G415) if that is at all helpful. Happy to provide more debug info as well if anyone has anything I should add here.

ANSWER

Answered 2022-Feb-16 at 20:18

They are not binding to the same port. One is binding to TCP on top of IPv6, the other is binding to TCP on top of IPv4.

To expand on the Java details a bit: new ServerSocket(port) in Java uses InetAddress.anyLocalAddress() because no InetAddress was passed in. InetAddress.anyLocalAddress() can return either an IPv4 or IPv6 address, which means this isn't guaranteed to be the same value to bind to across JVMs despite the same port being passed in.

Source https://stackoverflow.com/questions/71148158