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

Incorrect code in MulticastSocket sample code

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 13
    • 13
    • core-libs
    • None
    • b12

      In the documentation for MulticastSocket, the following sample code exists:

      String msg = "Hello";
      InetAddress group = InetAddress.getByName("228.5.6.7");
      MulticastSocket s = new MulticastSocket(6789);
      s.joinGroup(group);
      byte[] msgBytes = msg.getBytes();
      DatagramPacket hi = new DatagramPacket(msgBytes, msgBytes.length, group, 6789);

      The DatagramPacket's length argument takes the byte length and String.length() returns the number of UTF-16 characters, so if a string has any non-ASCII characters s.length() != s.getBytes().length.

            aeubanks Arthur Eubanks
            aeubanks Arthur Eubanks
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: