This patch extends the Socket class to support Multipath TCP (MPTCP) by:
1. Adding a new constructor that accepts an mptcp parameter to enable MPTCP
2. Introducing a new private Socket constructor variant that handles MPTCP
connections
3. Adding a createImpl factory method that propagates the MPTCP setting
to the underlying socket implementation
4. Maintaining backward compatibility while adding MPTCP capabilities
The implementation ensures MPTCP support is properly integrated with:
- Address resolution
- Socket binding
- Connection establishment
- Error handling
When MPTCP is enabled, the socket uses the MPTCP-enabled creation path
established in previous patches, while maintaining all existing socket
functionality for non-MPTCP cases.
Co-Developed-by: Gang Yan <yangang@kylinos.cn>
Signed-off-by: Gang Yan <yangang@kylinos.cn>
Signed-off-by: Geliang Tang <geliang@kernel.org>
---
.../share/classes/java/net/Socket.java | 88 +++++++++++++++++++
1 file changed, 88 insertions(+)
diff --git a/src/java.base/share/classes/java/net/Socket.java b/src/java.base/share/classes/java/net/Socket.java
index 692c3395f78..fc7bd5b98c0 100644
--- a/src/java.base/share/classes/java/net/Socket.java
+++ b/src/java.base/share/classes/java/net/Socket.java
@@ -403,6 +403,39 @@ public Socket(String host, int port, boolean stream) throws IOException {
(SocketAddress) null, stream);
}
+ /**
+ * Creates a stream socket and connects it to the specified port
+ * number on the named host using Multipath TCP (MPTCP) or TCP
+ * protocol.
+ * <p>
+ * If the specified host is {@code null} it is the equivalent of
+ * specifying the address as
+ * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
+ * In other words, it is equivalent to specifying an address of the
+ * loopback interface. </p>
+ * <p>
+ * If the application has specified a {@linkplain SocketImplFactory client
+ * socket implementation factory}, that factory's
+ * {@linkplain SocketImplFactory#createSocketImpl() createSocketImpl}
+ * method is called to create the actual socket implementation. Otherwise
+ * a system-default socket implementation is created.
+ *
+ * @param host the host name, or {@code null} for the loopback address.
+ * @param port the port number.
+ * @param stream must be true, false is not allowed.
+ * @param mptcp create a socket with MPTCP or TCP protocol.
+ * @throws IOException if an I/O error occurs when creating the socket.
+ * @throws IllegalArgumentException if the stream parameter is {@code false}
+ * or if the port parameter is outside the specified range of valid
+ * port values, which is between 0 and 65535, inclusive.
+ */
+ @SuppressWarnings("this-escape")
+ public Socket(String host, int port, boolean stream, boolean mptcp) throws IOException {
+ this(host != null ? new InetSocketAddress(host, port) :
+ new InetSocketAddress(InetAddress.getByName(null), port),
+ (SocketAddress) null, stream, mptcp);
+ }
+
/**
* Creates a socket and connects it to the specified port number at
* the specified IP address.
@@ -483,6 +516,61 @@ private static SocketImpl createImpl() {
}
}
+ /**
+ * Initialize a new Socket that is connected to the given remote address.
+ * The MPTCP or TCP protocol socket is optionally bound to a local address
+ * before connecting.
+ *
+ * @param address the remote address to connect to
+ * @param localAddr the local address to bind to, can be null
+ * @param stream true for a stream socket, false for a datagram socket
+ * @param mptcp create a socket with MPTCP or TCP protocol
+ */
+ private Socket(SocketAddress address, SocketAddress localAddr, boolean stream, boolean mptcp)
+ throws IOException
+ {
+ Objects.requireNonNull(address);
+ if (!stream) {
+ throw new IllegalArgumentException(
+ "Socket constructor does not support creation of datagram sockets");
+ }
+ assert address instanceof InetSocketAddress;
+
+ // create the SocketImpl and the underlying socket
+ SocketImpl impl = createImpl(mptcp);
+ impl.create(stream);
+
+ this.impl = impl;
+ this.state = SOCKET_CREATED;
+
+ try {
+ if (localAddr != null) {
+ bind(localAddr);
+ }
+ connect(address);
+ } catch (Throwable throwable) {
+ closeSuppressingExceptions(throwable);
+ throw throwable;
+ }
+ }
+
+ /**
+ * Create a new SocketImpl for a connecting/client socket. The SocketImpl
+ * is created without an underlying socket.
+ *
+ * @param mptcp create a socket with MPTCP or TCP protocol.
+ */
+ private static SocketImpl createImpl(boolean mptcp) {
+ SocketImplFactory factory = Socket.factory;
+ if (factory != null) {
+ return factory.createSocketImpl();
+ } else {
+ // create a SOCKS SocketImpl that delegates to a platform SocketImpl
+ SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false, mptcp);
+ return new SocksSocketImpl(delegate);
+ }
+ }
+
/**
* Returns the {@code SocketImpl} for this Socket, creating it, and the
* underlying socket, if required.
--
2.48.1
Hi Geliang, On 23/07/2025 07:16, Geliang Tang wrote: > This patch extends the Socket class to support Multipath TCP (MPTCP) by: > > 1. Adding a new constructor that accepts an mptcp parameter to enable MPTCP > 2. Introducing a new private Socket constructor variant that handles MPTCP > connections > 3. Adding a createImpl factory method that propagates the MPTCP setting > to the underlying socket implementation > 4. Maintaining backward compatibility while adding MPTCP capabilities Same here: the new helper could be called from the existing Socket() one instead of duplicating the code. Cheers, Matt -- Sponsored by the NGI0 Core fund.
© 2016 - 2026 Red Hat, Inc.