Incorrect code in MulticastSocket sample code

XMLWordPrintable

    • Type: Bug
    • Resolution: Fixed
    • Priority: P4
    • 13
    • Affects Version/s: 13
    • Component/s: 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.

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

              Created:
              Updated:
              Resolved: