8332623: Remove setTTL()/getTTL() methods from DatagramSocketImpl/MulticastSocket and MulticastSocket.send(DatagramPacket, byte)

Reviewed-by: dfuchs, alanb
This commit is contained in:
Jaikiran Pai 2025-06-17 08:32:17 +00:00
parent ff75f763c0
commit f7cd3fad24
19 changed files with 25 additions and 445 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -162,30 +162,6 @@ public abstract class DatagramSocketImpl implements SocketOptions {
*/
protected abstract void receive(DatagramPacket p) throws IOException;
/**
* Set the TTL (time-to-live) option.
* @param ttl a byte specifying the TTL value
*
* @deprecated use setTimeToLive instead.
* @throws IOException if an I/O exception occurs while setting
* the time-to-live option.
* @see #getTTL()
*/
@Deprecated(forRemoval = true, since = "1.2")
protected abstract void setTTL(byte ttl) throws IOException;
/**
* Retrieve the TTL (time-to-live) option.
*
* @throws IOException if an I/O exception occurs
* while retrieving the time-to-live option
* @deprecated use getTimeToLive instead.
* @return a byte representing the TTL value
* @see #setTTL(byte)
*/
@Deprecated(forRemoval = true, since = "1.2")
protected abstract byte getTTL() throws IOException;
/**
* Set the TTL (time-to-live) option.
* @param ttl an {@code int} specifying the time-to-live value

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -188,26 +188,6 @@ public class MulticastSocket extends DatagramSocket {
this(createDelegate(bindaddr, MulticastSocket.class));
}
/**
* Set the default time-to-live for multicast packets sent out
* on this {@code MulticastSocket} in order to control the
* scope of the multicasts.
*
* <p>The ttl is an <b>unsigned</b> 8-bit quantity, and so <B>must</B> be
* in the range {@code 0 <= ttl <= 0xFF }.
*
* @param ttl the time-to-live
* @throws IOException if an I/O exception occurs
* while setting the default time-to-live value, or the socket is closed.
* @deprecated use the {@link #setTimeToLive(int)} method instead, which uses
* <b>int</b> instead of <b>byte</b> as the type for ttl.
* @see #getTTL()
*/
@Deprecated(forRemoval = true, since = "1.2")
public void setTTL(byte ttl) throws IOException {
delegate().setTTL(ttl);
}
/**
* Set the default time-to-live for multicast packets sent out
* on this {@code MulticastSocket} in order to control the
@ -237,22 +217,6 @@ public class MulticastSocket extends DatagramSocket {
delegate().setTimeToLive(ttl);
}
/**
* Get the default time-to-live for multicast packets sent out on
* the socket.
*
* @throws IOException if an I/O exception occurs
* while getting the default time-to-live value, or the socket is closed.
* @return the default time-to-live value
* @deprecated use the {@link #getTimeToLive()} method instead,
* which returns an <b>int</b> instead of a <b>byte</b>.
* @see #setTTL(byte)
*/
@Deprecated(forRemoval = true, since = "1.2")
public byte getTTL() throws IOException {
return delegate().getTTL();
}
/**
* Get the default time-to-live for multicast packets sent out on
* the socket.
@ -466,47 +430,4 @@ public class MulticastSocket extends DatagramSocket {
public boolean getLoopbackMode() throws SocketException {
return delegate().getLoopbackMode();
}
/**
* Sends a datagram packet to the destination, with a TTL (time-to-live)
* other than the default for the socket. This method
* need only be used in instances where a particular TTL is desired;
* otherwise it is preferable to set a TTL once on the socket, and
* use that default TTL for all packets. This method does <B>not
* </B> alter the default TTL for the socket. Its behavior may be
* affected by {@code setInterface}.
*
* @param p is the packet to be sent. The packet should contain
* the destination multicast ip address and the data to be sent.
* One does not need to be the member of the group to send
* packets to a destination multicast address.
* @param ttl optional time to live for multicast packet.
* default ttl is 1.
*
* @throws IOException if an I/O error occurs, or the socket is closed.
* @throws PortUnreachableException may be thrown if the socket is connected
* to a currently unreachable destination. Note, there is no
* guarantee that the exception will be thrown.
* @throws IllegalArgumentException if the socket is connected,
* and connected address and packet address differ, or
* if the socket is not connected and the packet address
* is not set or if its port is out of range.
*
*
* @deprecated Use the following code or its equivalent instead:
* <pre>{@code ......
* int ttl = mcastSocket.getOption(StandardSocketOptions.IP_MULTICAST_TTL);
* mcastSocket.setOption(StandardSocketOptions.IP_MULTICAST_TTL, newttl);
* mcastSocket.send(p);
* mcastSocket.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);
* ......}</pre>
*
* @see DatagramSocket#send
* @see DatagramSocket#receive
*/
@Deprecated(forRemoval = true, since = "1.4")
public void send(DatagramPacket p, byte ttl)
throws IOException {
delegate().send(p, ttl);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -567,12 +567,6 @@ final class NetMulticastSocket extends MulticastSocket {
*/
private boolean interfaceSet;
/**
* The lock on the socket's TTL. This is for set/getTTL and
* send(packet,ttl).
*/
private final Object ttlLock = new Object();
/**
* The lock on the socket's interface - used by setInterface
* and getInterface
@ -584,14 +578,6 @@ final class NetMulticastSocket extends MulticastSocket {
*/
private InetAddress infAddress = null;
@Override
@SuppressWarnings("removal")
public void setTTL(byte ttl) throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
getImpl().setTTL(ttl);
}
@Override
public void setTimeToLive(int ttl) throws IOException {
if (ttl < 0 || ttl > 255) {
@ -602,14 +588,6 @@ final class NetMulticastSocket extends MulticastSocket {
getImpl().setTimeToLive(ttl);
}
@Override
@SuppressWarnings("removal")
public byte getTTL() throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
return getImpl().getTTL();
}
@Override
public int getTimeToLive() throws IOException {
if (isClosed())
@ -796,50 +774,4 @@ final class NetMulticastSocket extends MulticastSocket {
public boolean getLoopbackMode() throws SocketException {
return ((Boolean)getImpl().getOption(SocketOptions.IP_MULTICAST_LOOP)).booleanValue();
}
@SuppressWarnings("removal")
@Override
public void send(DatagramPacket p, byte ttl)
throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
synchronized(ttlLock) {
synchronized(p) {
InetAddress packetAddress = p.getAddress();
checkAddress(packetAddress, "send");
if (connectState == ST_NOT_CONNECTED) {
if (packetAddress == null) {
throw new IllegalArgumentException("Address not set");
}
} else {
// we're connected
if (packetAddress == null) {
p.setAddress(connectedAddress);
p.setPort(connectedPort);
} else if ((!packetAddress.equals(connectedAddress)) ||
p.getPort() != connectedPort) {
throw new IllegalArgumentException("connected address and packet address" +
" differ");
}
}
byte dttl = getTTL();
try {
if (ttl != dttl) {
// set the ttl
getImpl().setTTL(ttl);
}
if (p.getPort() == 0) {
throw new SocketException("Can't send to port 0");
}
// call the datagram method to send
getImpl().send(p);
} finally {
// set it back to default
if (ttl != dttl) {
getImpl().setTTL(dttl);
}
}
} // synch p
} //synch ttl
} //method
}

View File

@ -50,7 +50,6 @@ import java.nio.channels.DatagramChannel;
import java.nio.channels.MembershipKey;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static jdk.internal.util.Exceptions.formatMsg;
@ -372,44 +371,19 @@ public class DatagramSocketAdaptor
// -- java.net.MulticastSocket --
// used to coordinate changing TTL with the deprecated send method
private final ReentrantLock sendLock = new ReentrantLock();
// cached outgoing interface (for use by setInterface/getInterface)
private final Object outgoingInterfaceLock = new Object();
private NetworkInterface outgoingNetworkInterface;
private InetAddress outgoingInetAddress;
@Override
@SuppressWarnings("removal")
public void setTTL(byte ttl) throws IOException {
setTimeToLive(Byte.toUnsignedInt(ttl));
}
@Override
public void setTimeToLive(int ttl) throws IOException {
sendLock.lock();
try {
setIntOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);
} finally {
sendLock.unlock();
}
}
@Override
@SuppressWarnings("removal")
public byte getTTL() throws IOException {
return (byte) getTimeToLive();
setIntOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);
}
@Override
public int getTimeToLive() throws IOException {
sendLock.lock();
try {
return getIntOption(StandardSocketOptions.IP_MULTICAST_TTL);
} finally {
sendLock.unlock();
}
return getIntOption(StandardSocketOptions.IP_MULTICAST_TTL);
}
@Override
@ -558,23 +532,6 @@ public class DatagramSocketAdaptor
return !enabled;
}
@Override
@SuppressWarnings("removal")
public void send(DatagramPacket p, byte ttl) throws IOException {
sendLock.lock();
try {
int oldValue = getTimeToLive();
try {
setTTL(ttl);
send(p);
} finally {
setTimeToLive(oldValue);
}
} finally {
sendLock.unlock();
}
}
/**
* Returns the outgoing NetworkInterface or null if not set.
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -108,43 +108,5 @@ public class AddressNotSet {
p = new DatagramPacket(buf, buf.length, addr, port);
sock.send(p);
serversock.receive(p);
if (sock instanceof MulticastSocket) {
sock.disconnect();
testTTL((MulticastSocket)sock);
}
}
private void testTTL(MulticastSocket sock) throws Exception {
out.println("Testing deprecated send TTL with " + sock.getClass());
final byte ttl = 100;
InetAddress addr = loopbackAddress;
byte[] buf;
DatagramPacket p;
int port = serversock.getLocalPort();
out.println("Checking send to non-connected address ...");
try {
out.println("Checking send with no packet address");
buf = ("Hello, server"+(++i)).getBytes();
p = new DatagramPacket(buf, buf.length);
sock.send(p,ttl);
throw new AssertionError("Expected IllegalArgumentException not received");
} catch (IllegalArgumentException x) {
out.println("Got expected exception: " + x);
}
out.println("Connecting to connected address: " + sock);
sock.connect(addr, port);
try {
out.println("Checking send with different address than connected");
buf = ("Hello, server"+(++i)).getBytes();
p = new DatagramPacket(buf, buf.length, addr, port+1);
sock.send(p, ttl);
throw new AssertionError("Expected IllegalArgumentException not received");
} catch (IllegalArgumentException x) {
out.println("Got expected exception: " + x);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -252,7 +252,7 @@ public class DatagramSocketMulticasting {
}
/**
* Test getTimeToLive/setTimeToLive/getTTL/getTTL and IP_MULTICAST_TTL socket
* Test getTimeToLive/setTimeToLive and IP_MULTICAST_TTL socket
* option.
*/
static void testTimeToLive(DatagramSocket s) throws IOException {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -121,14 +121,6 @@ public class OldDatagramSocketImplTest {
@Override
protected void receive(DatagramPacket p) throws IOException { }
@Override
protected void setTTL(byte ttl) throws IOException { }
@Override
protected byte getTTL() throws IOException {
return 0;
}
@Override
protected void setTimeToLive(int ttl) throws IOException { }

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -45,7 +45,7 @@ import static org.testng.Assert.expectThrows;
* @bug 8236105
* @summary Check that DatagramSocket, MulticastSocket,
* DatagramSocketAdaptor and DatagramChannel all
* throw expected Execption when passed a DatagramPacket
* throw expected Exception when passed a DatagramPacket
* with invalid details
* @run testng SendCheck
*/
@ -126,11 +126,9 @@ public class SendCheck {
List<Sender> senders = List.of(
Sender.of(new DatagramSocket(null)),
Sender.of(new MulticastSocket(null), (byte) 0),
Sender.of(new MulticastSocket(null)),
Sender.of(DatagramChannel.open()),
Sender.of(DatagramChannel.open().socket()),
Sender.of((MulticastSocket)
DatagramChannel.open().socket(), (byte) 0)
Sender.of(DatagramChannel.open().socket())
);
List<Object[]> testcases = new ArrayList<>();
@ -179,12 +177,6 @@ public class SendCheck {
return new SenderImpl<>(socket, socket::send, socket::close, SE);
}
static Sender<IOException> of(MulticastSocket socket, byte ttl) {
SenderImpl.Send<IOException> send =
(pkt) -> socket.send(pkt, ttl);
return new SenderImpl<>(socket, send, socket::close, SE);
}
static Sender<IOException> of(DatagramChannel socket) {
SenderImpl.Send<IOException> send =
(pkt) -> socket.send(ByteBuffer.wrap(pkt.getData()),

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -63,15 +63,6 @@ public class MyDatagramSocketImplFactory implements DatagramSocketImplFactory {
ds.receive(p);
}
@Override
protected void setTTL(byte ttl) throws IOException {
}
@Override
protected byte getTTL() throws IOException {
return 0;
}
@Override
protected void setTimeToLive(int ttl) throws IOException {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -105,8 +105,6 @@ public class TestCreate {
@Override protected int peek(InetAddress i) { return 0; }
@Override protected int peekData(DatagramPacket p) { return 0; }
@Override protected void receive(DatagramPacket p) { }
@Override protected void setTTL(byte ttl) { }
@Override protected byte getTTL() { return 0; }
@Override protected void setTimeToLive(int ttl) { }
@Override protected int getTimeToLive() { return 0; }
@Override protected void join(InetAddress inetaddr) { }

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -99,8 +99,6 @@ public class TestDefaultBehavior {
@Override protected int peek(InetAddress i) { return 0; }
@Override protected int peekData(DatagramPacket p) { return 0; }
@Override protected void receive(DatagramPacket p) { }
@Override protected void setTTL(byte ttl) { }
@Override protected byte getTTL() { return 0; }
@Override protected void setTimeToLive(int ttl) { }
@Override protected int getTimeToLive() { return 0; }
@Override protected void join(InetAddress inetaddr) { }

View File

@ -1,61 +0,0 @@
/*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4089415
* @summary Test MulticastSocket send for modification of ttl
*
*/
import java.io.*;
import java.net.*;
public class MulticastTTL {
public static void main(String args[]) throws Exception {
MulticastSocket soc = null;
DatagramPacket pac = null;
InetAddress sin = null;
byte [] array = new byte[65537];
int port = 0;
byte old_ttl = 0;
byte new_ttl = 64;
byte ttl = 0;
sin = InetAddress.getByName("224.80.80.80");
soc = new MulticastSocket();
port = soc.getLocalPort();
old_ttl = soc.getTTL();
pac = new DatagramPacket(array, array.length, sin, port);
try {
soc.send(pac, new_ttl);
} catch(java.io.IOException e) {
ttl = soc.getTTL();
soc.close();
if(ttl != old_ttl)
throw new RuntimeException("TTL ");
}
soc.close();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -93,7 +93,6 @@ public class SendPortZero {
@Test(dataProvider = "data")
public void testSend(MulticastSocket ms, DatagramPacket pkt) {
assertThrows(SE, () -> ms.send(pkt));
assertThrows(SE, () -> ms.send(pkt, (byte) 0));
}
@AfterTest

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -206,16 +206,6 @@ public class SetLoopbackOption {
shouldNotComeHere();
}
@Override
protected void setTTL(byte ttl) throws IOException {
shouldNotComeHere();
}
@Override
protected byte getTTL() throws IOException {
return shouldNotComeHere();
}
@Override
protected void setTimeToLive(int ttl) throws IOException {
shouldNotComeHere();

View File

@ -1,48 +0,0 @@
/*
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4189640
* @summary Make setTTL/getTTL works
* @run main SetTTLAndGetTTL
*/
import java.net.*;
public class SetTTLAndGetTTL {
public static void main(String args[]) throws Exception {
MulticastSocket soc = null;
try {
soc = new MulticastSocket();
} catch(Exception e) {
throw new Exception("Unexpected Exception");
}
soc.setTTL((byte)200);
byte ttlValue = soc.getTTL();
if (ttlValue != (byte)200)
throw new Exception("setTTL/getTTL is broken");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -40,7 +40,6 @@ public class SetTTLTo0 {
throw new Exception("Unexpected Exception");
}
soc.setTTL((byte)0);
soc.setTimeToLive(0);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -124,8 +124,6 @@ public class CachedImplOptions {
@Override protected int peek(InetAddress i) { return 0; }
@Override protected int peekData(DatagramPacket p) { return 0; }
@Override protected void receive(DatagramPacket p) { }
@Override protected void setTTL(byte ttl) { }
@Override protected byte getTTL() { return 0; }
@Override protected void setTimeToLive(int ttl) { }
@Override protected int getTimeToLive() { return 0; }
@Override protected void join(InetAddress inetaddr) { }

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -162,10 +162,6 @@ public class ImmutableOptions {
protected void receive(DatagramPacket p) throws IOException { }
protected void setTTL(byte ttl) throws IOException { }
protected byte getTTL() throws IOException { return 0; }
protected void setTimeToLive(int ttl) throws IOException { }
protected int getTimeToLive() throws IOException { return 0; }

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -327,39 +327,27 @@ public class AdaptorMulticasting {
}
/**
* Test getTimeToLive/setTimeToLive/getTTL/getTTL and IP_MULTICAST_TTL socket
* Test getTimeToLive/setTimeToLive and IP_MULTICAST_TTL socket
* option.
*/
static void testTimeToLive(MulticastSocket s) throws IOException {
// should be 1 by default
assertTrue(s.getTimeToLive() == 1);
assertTrue(s.getTTL() == 1);
assertTrue(s.getOption(IP_MULTICAST_TTL) == 1);
// setTimeToLive
for (int ttl = 0; ttl <= 2; ttl++) {
s.setTimeToLive(ttl);
assertTrue(s.getTimeToLive() == ttl);
assertTrue(s.getTTL() == ttl);
assertTrue(s.getOption(IP_MULTICAST_TTL) == ttl);
}
assertThrows(IllegalArgumentException.class, () -> s.setTimeToLive(-1));
// setTTL
for (byte ttl = (byte) -2; ttl <= 2; ttl++) {
s.setTTL(ttl);
assertTrue(s.getTTL() == ttl);
int intValue = Byte.toUnsignedInt(ttl);
assertTrue(s.getTimeToLive() == intValue);
assertTrue(s.getOption(IP_MULTICAST_TTL) == intValue);
}
// setOption(IP_MULTICAST_TTL)
for (int ttl = 0; ttl <= 2; ttl++) {
s.setOption(IP_MULTICAST_TTL, ttl);
assertTrue(s.getOption(IP_MULTICAST_TTL) == ttl);
assertTrue(s.getTimeToLive() == ttl);
assertTrue(s.getTTL() == ttl);
}
// bad values for IP_MULTICAST_TTL
@ -419,7 +407,7 @@ public class AdaptorMulticasting {
// send message to multicast group
DatagramPacket p = new DatagramPacket(message, message.length);
p.setSocketAddress(target);
s.send(p, (byte) 1);
s.send(p);
// receive message
s.setSoTimeout(0);
@ -463,7 +451,7 @@ public class AdaptorMulticasting {
// send datagram to multicast group
DatagramPacket p = new DatagramPacket(message, message.length);
p.setSocketAddress(target);
s.send(p, (byte) 1);
s.send(p);
// datagram should not be received
s.setSoTimeout(500);