JDK-8260667 : Update DatagramSocket to add support for joining multicast groups
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.net
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 17
  • Submitted: 2021-01-29
  • Updated: 2021-04-21
  • Resolved: 2021-02-05
Related Reports
CSR :  
Description
Summary
-------

Allow `DatagramSocket` to be used both for sending and receiving multicast datagrams.
We propose to "move" `joinGroup(SocketAddress, NetworkInterface)` and `leaveGroup(SocketAddress, NetworkInterface)` up from `MulticastSocket` into `DatagramSocket`.

Problem
-------

`DatagramSocket` has a `DatagramSocket.setDatagramSocketImplFactory` method that allows to globally replace the default `DatagramSocket`/`MulticastSocket` implementation provided by the JDK. This was provided as a way in early JDK releases to replace the system wide implementation. It has been mostly obsolete since Java 1.4. A `DatagramSocket` can be created to use a custom implementation by extending `DatagramSocket` and using the protected constructor that takes the impl as a parameter. However, `MulticastSocket` doesn't provide such a constructor.

Though `DatagramSocket` can be subclassed to provide a custom implementation, `MulticastSocket`, if subclassed, will still create its default implementation, even when all methods of `MulticastSocket` are overridden in order not to use it. This will create a file descriptor / socket leak.

The only solution to avoid that is currently to replace the default `DatagramSocketImplFactory` by calling the static `DatagramSocket.setDatagramSocketImplFactory`. We need a better solution.

Solution
--------

The solution proposed in this change is to allow DatagramSocket to both send and receive multicast datagrams. An application that need to completely replace the default multicast implementation, and that cannot be easily updated to use DatagramChannel, can do so by subclassing DatagramSocket instead. 

Specification
-------------

 - move `joinGroup(SocketAddress, NetworkInterface)` and `leaveGroup(SocketAddress, NetworkInterface)` up from `MulticastSocket` into `DatagramSocket`.
 - improve `DatagramSocket` with an API note to describe how `DatgramSocket` can be used directly for multicasting.
 - add an `@apiNote` to the various convenience getters/setters defined in `DatagramSocket` and `MulticastSocket` to show how they map to standard socket options.

Overrides of the two methods hoisted to `DatagramSocket` are kept in `MulticastSocket` for the purpose of keeping `@since` properly documented.

A zip of the specdiff will be attached before this CSR is finalized. In the meantime, and for convenience, the specdiff can be browsed online at:

[http://cr.openjdk.java.net/~dfuchs/ds-ms-8237352-specdiff.07/overview-summary.html][1]


  [1]: http://cr.openjdk.java.net/~dfuchs/ds-ms-8237352-specdiff.07/overview-summary.html
Comments
Moving to Approved.
05-02-2021

Attached specdiff-07.zip which contains specdiff, generated API doc for MulticastSocket and DatagramSocket, and webrev (and an index.html file in the root dir to point at the various pieces).
02-02-2021

I haven't found any evidence of interest in replacing the underlying implementation of MulticastSocket but hoisting the methods as proposal fixes a long standing anomaly in the API where DS can send multicast datagrams but not join multicast groups (except by way of binding to a multicast address). The proposed change also opens the door for several simplifications in the future so I think it's a good change.
01-02-2021