diff --git a/src/java.base/share/classes/java/net/DatagramSocketImpl.java b/src/java.base/share/classes/java/net/DatagramSocketImpl.java index 54093a9d5f1..71db2c0a427 100644 --- a/src/java.base/share/classes/java/net/DatagramSocketImpl.java +++ b/src/java.base/share/classes/java/net/DatagramSocketImpl.java @@ -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 diff --git a/src/java.base/share/classes/java/net/MulticastSocket.java b/src/java.base/share/classes/java/net/MulticastSocket.java index 367aaf3a9ea..ac1b40c2767 100644 --- a/src/java.base/share/classes/java/net/MulticastSocket.java +++ b/src/java.base/share/classes/java/net/MulticastSocket.java @@ -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. - * - *
The ttl is an unsigned 8-bit quantity, and so must 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 - * int instead of byte 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 int instead of a byte. - * @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 not - * 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: - *
{@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);
- * ......}
- *
- * @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);
- }
}
diff --git a/src/java.base/share/classes/java/net/NetMulticastSocket.java b/src/java.base/share/classes/java/net/NetMulticastSocket.java
index bce2e945335..80cb997102f 100644
--- a/src/java.base/share/classes/java/net/NetMulticastSocket.java
+++ b/src/java.base/share/classes/java/net/NetMulticastSocket.java
@@ -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
}
diff --git a/src/java.base/share/classes/sun/nio/ch/DatagramSocketAdaptor.java b/src/java.base/share/classes/sun/nio/ch/DatagramSocketAdaptor.java
index 44b05f97472..a9962d19400 100644
--- a/src/java.base/share/classes/sun/nio/ch/DatagramSocketAdaptor.java
+++ b/src/java.base/share/classes/sun/nio/ch/DatagramSocketAdaptor.java
@@ -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.
*/
diff --git a/test/jdk/java/net/DatagramSocket/AddressNotSet.java b/test/jdk/java/net/DatagramSocket/AddressNotSet.java
index 0e7b28aa044..afab2c686d1 100644
--- a/test/jdk/java/net/DatagramSocket/AddressNotSet.java
+++ b/test/jdk/java/net/DatagramSocket/AddressNotSet.java
@@ -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);
- }
}
}
diff --git a/test/jdk/java/net/DatagramSocket/DatagramSocketMulticasting.java b/test/jdk/java/net/DatagramSocket/DatagramSocketMulticasting.java
index 1758ff2190c..39cdf7210f7 100644
--- a/test/jdk/java/net/DatagramSocket/DatagramSocketMulticasting.java
+++ b/test/jdk/java/net/DatagramSocket/DatagramSocketMulticasting.java
@@ -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 {
diff --git a/test/jdk/java/net/DatagramSocket/OldDatagramSocketImplTest.java b/test/jdk/java/net/DatagramSocket/OldDatagramSocketImplTest.java
index c1a110c6619..7f8eea874ec 100644
--- a/test/jdk/java/net/DatagramSocket/OldDatagramSocketImplTest.java
+++ b/test/jdk/java/net/DatagramSocket/OldDatagramSocketImplTest.java
@@ -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 { }
diff --git a/test/jdk/java/net/DatagramSocket/SendCheck.java b/test/jdk/java/net/DatagramSocket/SendCheck.java
index 8943e8517fa..482eadb3cf9 100644
--- a/test/jdk/java/net/DatagramSocket/SendCheck.java
+++ b/test/jdk/java/net/DatagramSocket/SendCheck.java
@@ -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