Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8331657

DatagramSocket.connect() throws an exception when port=0

XMLWordPrintable

    • 17
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Windows/Linux
      JDK 21/22

      A DESCRIPTION OF THE PROBLEM :

      The call to DatagramSocket.connect() throws an exception when port=0.
      The parameter port=0 should be possible (as a wildcard) if one wants to receive UDP packets from a particular host without specifying the source port.
      Parameter Port=0 was possible in JDK8 and JDK11.
      The underlying OS layers allow port=0 at least on Windows, Linux and BSD.

      The exception should only be thrown when one tries to send a datagram.
      As long as one only wants to receive datagrams , connect() with port=0 should be accepted.

      REGRESSION : Last worked in version 11.0.23

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - create a DatagramSocket bound to any local port.
      - try to connect to a remote host, with port=0

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The call to connect() should succeed.
      ACTUAL -
      An exception is thrown:
      Exception in thread "main" java.io.UncheckedIOException: java.net.SocketException: Can't connect to port 0


      ---------- BEGIN SOURCE ----------
      import java.net.*;

      public class T
      {

          static public void main(String [] args) throws SocketException, UnknownHostException, InterruptedException
          {
              DatagramSocket T=new DatagramSocket(12345);
              T.connect(InetAddress.getByName("192.168.0.2"),0);
              System.out.println("Pass");
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No work around found.


      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: