JDK-8236807 : DatagramSocket/DatagramChannel::connect and DatagramSocket/DatagramChannel::send could explicitly use localhost when the specified peer address is the wildcard
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.net
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2020-01-08
  • Updated: 2023-07-24
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
Rather than relying on undocumented kernel specific behavior, DatagramSocket::connect and DatagramSocket::send could explicitly use localhost when the specified peer address is the wildcard (and same for DatagramChannel and its socket adaptor).

This would allow to do things like:

var listener = new DatagramSocket(0);
var sender = new DatagramSocket(0);
var packet = new DatagramPacket(buf, buf.length);
sender.connect(listener.getLocalSocketAddress());
sender.send(packet);

which currently only works on mac when -Djava.net.preferIPv4Stack is specified.
Comments
test/jdk/java/net/DatagramSocket/SendPortZero.java test/jdk/java/net/MulticastSocket/SendPortZero.java also test this scenario, so should be updated too
24-07-2023

test/jdk/java/net/DatagramSocket/SendCheck.java This test should be revisited when fixing this (JDK-8236807) issue. /* Commented until JDK-8236807 is fixed // wildcard addr w/valid port -- DS, MS throws IO ; DC, DSA doesn't throw var pkt5 = new DatagramPacket(buf, 0, buf.length); var addr1 = wildcard.getAddress(); pkt5.setAddress(addr1); pkt5.setPort(socket.getLocalPort()); */
14-04-2020