:p
atchew
Login
The Multipath TCP (MPTCP) protocol (v1 / RFC 8684) has been added in the upstream Linux kernel since v5.6. This patch series introduces comprehensive Multipath TCP (MPTCP) support to the Java Networking API, enabling applications to leverage MPTCP's capabilities for improved reliability and throughput. The implementation provides: 1. Core infrastructure for MPTCP socket creation (Patch 1) 2. Server and client socket creation APIs (Patches 2-3) 3. Integration with NIO socket implementation (Patch 4) 4. High-level Socket and ServerSocket support (Patches 5-6) 5. Comprehensive test cases (Patch 7) Key features: - Backward compatible API extensions - Platform-independent implementation - Explicit MPTCP enablement through new constructors - Full integration with existing socket functionality - Test coverage for basic MPTCP operations The changes maintain all existing functionality while adding MPTCP capabilities. When MPTCP is not enabled or not supported by the system, the implementation gracefully falls back to regular TCP behavior. This work enables Java applications to benefit from MPTCP's capabilities: - Seamless handover between network interfaces - Aggregated bandwidth from multiple paths - Improved resilience to network failures The implementation has been tested on Linux systems with MPTCP kernel support enabled. https://github.com/openjdk/jdk Geliang Tang (7): Add MPTCP support for socket creation Add MPTCP server socket creation support Add MPTCP client socket creation support Add MPTCP support to NioSocketImpl Add MPTCP support to ServerSocket class Add MPTCP support to Socket class Add test cases for MPTCP socket functionality .../share/classes/java/net/ServerSocket.java | 66 ++++++++++++++ .../share/classes/java/net/Socket.java | 88 +++++++++++++++++++ .../share/classes/java/net/SocketImpl.java | 8 ++ .../share/classes/sun/nio/ch/Net.java | 26 +++++- .../classes/sun/nio/ch/NioSocketImpl.java | 19 +++- src/java.base/unix/native/libnio/ch/Net.c | 5 +- src/java.base/windows/native/libnio/ch/Net.c | 2 +- .../java/net/ServerSocket/MPTCPServer.java | 58 ++++++++++++ test/jdk/java/net/Socket/MPTCPClient.java | 62 +++++++++++++ 9 files changed, 326 insertions(+), 8 deletions(-) create mode 100644 test/jdk/java/net/ServerSocket/MPTCPServer.java create mode 100644 test/jdk/java/net/Socket/MPTCPClient.java -- 2.48.1
This patch extends the socket creation functionality to support Multipath TCP (MPTCP) by adding a new 'mptcp' parameter to the native socket0 method. When enabled, the socket will be created with the IPPROTO_MPTCP protocol for stream sockets. The changes include: 1. Added the 'mptcp' parameter to socket0 method declarations in Net.java 2. Modified the native implementations in both Unix and Windows Net.c files to handle the new parameter and set the appropriate protocol 3. Updated all call sites to pass 'false' for the new parameter to maintain existing behavior (MPTCP disabled by default) Co-Developed-by: Gang Yan <yangang@kylinos.cn> Signed-off-by: Gang Yan <yangang@kylinos.cn> Signed-off-by: Geliang Tang <geliang@kernel.org> --- src/java.base/share/classes/sun/nio/ch/Net.java | 6 +++--- src/java.base/unix/native/libnio/ch/Net.c | 5 +++-- src/java.base/windows/native/libnio/ch/Net.c | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/java.base/share/classes/sun/nio/ch/Net.java b/src/java.base/share/classes/sun/nio/ch/Net.java index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/sun/nio/ch/Net.java +++ b/src/java.base/share/classes/sun/nio/ch/Net.java @@ -XXX,XX +XXX,XX @@ static FileDescriptor socket() throws IOException { static FileDescriptor socket(ProtocolFamily family, boolean stream) throws IOException { boolean preferIPv6 = isIPv6Available() && (family != StandardProtocolFamily.INET); - return IOUtil.newFD(socket0(preferIPv6, stream, false, FAST_LOOPBACK)); + return IOUtil.newFD(socket0(preferIPv6, stream, false, FAST_LOOPBACK, false)); } static FileDescriptor serverSocket() { @@ -XXX,XX +XXX,XX @@ static FileDescriptor serverSocket() { static FileDescriptor serverSocket(ProtocolFamily family) { boolean preferIPv6 = isIPv6Available() && (family != StandardProtocolFamily.INET); - return IOUtil.newFD(socket0(preferIPv6, true, true, FAST_LOOPBACK)); + return IOUtil.newFD(socket0(preferIPv6, true, true, FAST_LOOPBACK, false)); } // Due to oddities SO_REUSEADDR on Windows reuse is ignored private static native int socket0(boolean preferIPv6, boolean stream, boolean reuse, - boolean fastLoopback); + boolean fastLoopback, boolean mptcp); public static void bind(FileDescriptor fd, InetAddress addr, int port) throws IOException diff --git a/src/java.base/unix/native/libnio/ch/Net.c b/src/java.base/unix/native/libnio/ch/Net.c index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/unix/native/libnio/ch/Net.c +++ b/src/java.base/unix/native/libnio/ch/Net.c @@ -XXX,XX +XXX,XX @@ Java_sun_nio_ch_Net_canUseIPv6OptionsWithIPv4LocalAddress0(JNIEnv* env, jclass c JNIEXPORT jint JNICALL Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6, - jboolean stream, jboolean reuse, jboolean ignored) + jboolean stream, jboolean reuse, jboolean ignored, jboolean mptcp) { int fd; int type = (stream ? SOCK_STREAM : SOCK_DGRAM); int domain = (ipv6_available() && preferIPv6) ? AF_INET6 : AF_INET; + int protocol = (stream && mptcp) ? IPPROTO_MPTCP : 0; - fd = socket(domain, type, 0); + fd = socket(domain, type, protocol); if (fd < 0) { return handleSocketError(env, errno); } diff --git a/src/java.base/windows/native/libnio/ch/Net.c b/src/java.base/windows/native/libnio/ch/Net.c index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/windows/native/libnio/ch/Net.c +++ b/src/java.base/windows/native/libnio/ch/Net.c @@ -XXX,XX +XXX,XX @@ Java_sun_nio_ch_Net_canUseIPv6OptionsWithIPv4LocalAddress0(JNIEnv* env, jclass c JNIEXPORT jint JNICALL Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6, - jboolean stream, jboolean reuse, jboolean fastLoopback) + jboolean stream, jboolean reuse, jboolean fastLoopback, jboolean mptcp) { SOCKET s; int domain = (preferIPv6) ? AF_INET6 : AF_INET; -- 2.48.1
This patch introduces new methods to create server sockets with Multipath TCP (MPTCP) support: 1. Added an overloaded 'serverSocket' method that accepts an 'mptcp' parameter to enable MPTCP 2. Added a convenience method 'mptcpServerSocket()' for creating MPTCP-enabled server sockets using the unspecified protocol family by default The changes maintain backward compatibility while providing explicit MPTCP support for server socket creation. The new methods leverage the existing socket0 infrastructure with the MPTCP flag added in the previous patch. Co-Developed-by: Gang Yan <yangang@kylinos.cn> Signed-off-by: Gang Yan <yangang@kylinos.cn> Signed-off-by: Geliang Tang <geliang@kernel.org> --- src/java.base/share/classes/sun/nio/ch/Net.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/java.base/share/classes/sun/nio/ch/Net.java b/src/java.base/share/classes/sun/nio/ch/Net.java index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/sun/nio/ch/Net.java +++ b/src/java.base/share/classes/sun/nio/ch/Net.java @@ -XXX,XX +XXX,XX @@ static FileDescriptor serverSocket(ProtocolFamily family) { return IOUtil.newFD(socket0(preferIPv6, true, true, FAST_LOOPBACK, false)); } + static FileDescriptor serverSocket(ProtocolFamily family, boolean mptcp) { + boolean preferIPv6 = isIPv6Available() && + (family != StandardProtocolFamily.INET); + return IOUtil.newFD(socket0(preferIPv6, true, true, FAST_LOOPBACK, mptcp)); + } + + static FileDescriptor mptcpServerSocket() { + return serverSocket(UNSPEC, true); + } + // Due to oddities SO_REUSEADDR on Windows reuse is ignored private static native int socket0(boolean preferIPv6, boolean stream, boolean reuse, boolean fastLoopback, boolean mptcp); -- 2.48.1
This patch extends the socket creation API to support Multipath TCP (MPTCP) for client sockets: 1. Added an overloaded 'socket' method that accepts an 'mptcp' parameter to explicitly enable/disable MPTCP 2. Introduced a convenience method 'mptcpSocket()' for creating MPTCP-enabled client sockets using the unspecified protocol family by default The changes maintain consistency with the existing socket API while providing first-class support for MPTCP client sockets. The implementation builds upon the foundation laid in previous patches, utilizing the socket0 infrastructure with MPTCP protocol support. Co-Developed-by: Gang Yan <yangang@kylinos.cn> Signed-off-by: Gang Yan <yangang@kylinos.cn> Signed-off-by: Geliang Tang <geliang@kernel.org> --- src/java.base/share/classes/sun/nio/ch/Net.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/java.base/share/classes/sun/nio/ch/Net.java b/src/java.base/share/classes/sun/nio/ch/Net.java index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/sun/nio/ch/Net.java +++ b/src/java.base/share/classes/sun/nio/ch/Net.java @@ -XXX,XX +XXX,XX @@ static FileDescriptor socket(ProtocolFamily family, boolean stream) throws IOExc return IOUtil.newFD(socket0(preferIPv6, stream, false, FAST_LOOPBACK, false)); } + static FileDescriptor socket(ProtocolFamily family, boolean stream, boolean mptcp) throws IOException { + boolean preferIPv6 = isIPv6Available() && + (family != StandardProtocolFamily.INET); + return IOUtil.newFD(socket0(preferIPv6, stream, false, FAST_LOOPBACK, mptcp)); + } + + static FileDescriptor mptcpSocket() throws IOException { + return socket(UNSPEC, true, true); + } + static FileDescriptor serverSocket() { return serverSocket(UNSPEC); } -- 2.48.1
This patch integrates MPTCP support into the core socket implementation by: 1. Adding a new createPlatformSocketImpl factory method that accepts an mptcp parameter 2. Extending NioSocketImpl to track MPTCP state with a new mptcp field 3. Modifying socket creation logic to use the appropriate MPTCP-enabled methods: - Uses Net.mptcpServerSocket() for server sockets when MPTCP is enabled - Uses Net.mptcpSocket() for client sockets when MPTCP is enabled The changes maintain backward compatibility while adding first-class MPTCP support to the socket implementation layer. The implementation builds upon the MPTCP infrastructure added in previous patches. 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/SocketImpl.java | 8 ++++++++ .../classes/sun/nio/ch/NioSocketImpl.java | 19 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/java.base/share/classes/java/net/SocketImpl.java b/src/java.base/share/classes/java/net/SocketImpl.java index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/java/net/SocketImpl.java +++ b/src/java.base/share/classes/java/net/SocketImpl.java @@ -XXX,XX +XXX,XX @@ static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(bo return (S) new NioSocketImpl(server); } + /** + * Creates an instance of platform's SocketImpl + */ + @SuppressWarnings("unchecked") + static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(boolean server, boolean mptcp) { + return (S) new NioSocketImpl(server, mptcp); + } + /** * The file descriptor object for this socket. */ diff --git a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java @@ -XXX,XX +XXX,XX @@ public final class NioSocketImpl extends SocketImpl implements PlatformSocketImp // true if this is a SocketImpl for a ServerSocket private final boolean server; + // true if this is a SocketImpl for a MptcpServerSocket + private final boolean mptcp; + // Lock held when reading (also used when accepting or connecting) private final ReentrantLock readLock = new ReentrantLock(); @@ -XXX,XX +XXX,XX @@ public final class NioSocketImpl extends SocketImpl implements PlatformSocketImp */ public NioSocketImpl(boolean server) { this.server = server; + this.mptcp = false; + } + + /** + * @param mptcp enable MPTCP + * + * Creates an instance of this SocketImpl. + * @param server true if this is a SocketImpl for a ServerSocket + */ + public NioSocketImpl(boolean server, boolean mptcp) { + this.server = server; + this.mptcp = mptcp; } /** @@ -XXX,XX +XXX,XX @@ protected void create(boolean stream) throws IOException { throw new IOException("Already created"); FileDescriptor fd; if (server) { - fd = Net.serverSocket(); + fd = mptcp ? Net.mptcpServerSocket() : Net.serverSocket(); } else { - fd = Net.socket(); + fd = mptcp ? Net.mptcpSocket() : Net.socket(); } Runnable closer = closerFor(fd); this.fd = fd; -- 2.48.1
This patch extends ServerSocket to support Multipath TCP (MPTCP) by: 1. Adding a new constructor that accepts an mptcp parameter to enable MPTCP 2. Introducing a new createImpl factory method that propagates the MPTCP setting to the underlying socket implementation 3. Maintaining all existing functionality while adding MPTCP capabilities The new constructor follows the same pattern as existing ServerSocket constructors but adds the mptcp parameter to control protocol selection. When enabled, the implementation uses the MPTCP-enabled socket creation path established in previous patches. 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/ServerSocket.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/java.base/share/classes/java/net/ServerSocket.java b/src/java.base/share/classes/java/net/ServerSocket.java index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/java/net/ServerSocket.java +++ b/src/java.base/share/classes/java/net/ServerSocket.java @@ -XXX,XX +XXX,XX @@ public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOExcept } } + /** + * Create a server using <i>mptcp</i> (Multipath TCP) or TCP protocol with + * the specified port, listen backlog, and local IP address to bind to. + * The <i>bindAddr</i> argument can be used on a multi-homed host for a + * ServerSocket that will only accept connect requests to one of its addresses. + * If <i>bindAddr</i> is null, it will default accepting + * connections on any/all local addresses. + * The port must be between 0 and 65535, inclusive. + * A port number of {@code 0} means that the port number is + * automatically allocated, typically from an ephemeral port range. + * This port number can then be retrieved by calling + * {@link #getLocalPort getLocalPort}. + * + * The {@code backlog} argument is the requested maximum number of + * pending connections on the socket. Its exact semantics are implementation + * specific. In particular, an implementation may impose a maximum length + * or may choose to ignore the parameter altogether. The value provided + * should be greater than {@code 0}. If it is less than or equal to + * {@code 0}, then an implementation specific default will be used. + * + * The {@code mptcp} argument is used to control whether to create a socket + * with MPTCP or TCP protocol. + * + * @param port the port number, or {@code 0} to use a port + * number that is automatically allocated. + * @param backlog requested maximum length of the queue of incoming + * connections. + * @param bindAddr the local InetAddress the server will bind to + * @param mptcp create a socket with MPTCP or TCP protocol. + * + * @throws IOException if an I/O error occurs when opening the socket. + * @throws IllegalArgumentException if the port parameter is outside + * the specified range of valid port values, which is between + * 0 and 65535, inclusive. + */ + @SuppressWarnings("this-escape") + public ServerSocket(int port, int backlog, InetAddress bindAddr, boolean mptcp) throws IOException { + if (port < 0 || port > 0xFFFF) + throw new IllegalArgumentException("Port value out of range: " + port); + if (backlog < 1) + backlog = 50; + + this.impl = createImpl(mptcp); + try { + bind(new InetSocketAddress(bindAddr, port), backlog); + } catch (IOException e) { + close(); + throw e; + } + } + /** * Create a SocketImpl for a server socket. The SocketImpl is created * without an underlying socket. @@ -XXX,XX +XXX,XX @@ private static SocketImpl createImpl() { } } + /** + * Create a SocketImpl for a server 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 = ServerSocket.factory; + if (factory != null) { + return factory.createSocketImpl(); + } else { + return SocketImpl.createPlatformSocketImpl(true, mptcp); + } + } + /** * Returns the {@code SocketImpl} for this ServerSocket, creating the * underlying socket if required. -- 2.48.1
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 XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/java/net/Socket.java +++ b/src/java.base/share/classes/java/net/Socket.java @@ -XXX,XX +XXX,XX @@ 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. @@ -XXX,XX +XXX,XX @@ 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
This patch introduces comprehensive test coverage for the newly added MPTCP socket support by: 1. Adding MPTCPServer.java - A test server that: - Creates an MPTCP-enabled ServerSocket - Accepts client connections - Echoes received messages back to clients - Handles graceful shutdown 2. Adding MPTCPClient.java - A test client that: - Connects to the MPTCP server - Sends user input to server - Displays server responses - Supports graceful termination The tests verify: - Basic MPTCP socket creation and connection - Bidirectional communication - Proper stream handling - Error conditions - Clean resource management Both test programs demonstrate the usage of the new MPTCP-enabled constructors added in previous patches. Co-Developed-by: Gang Yan <yangang@kylinos.cn> Signed-off-by: Gang Yan <yangang@kylinos.cn> Signed-off-by: Geliang Tang <geliang@kernel.org> --- .../java/net/ServerSocket/MPTCPServer.java | 58 +++++++++++++++++ test/jdk/java/net/Socket/MPTCPClient.java | 62 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 test/jdk/java/net/ServerSocket/MPTCPServer.java create mode 100644 test/jdk/java/net/Socket/MPTCPClient.java diff --git a/test/jdk/java/net/ServerSocket/MPTCPServer.java b/test/jdk/java/net/ServerSocket/MPTCPServer.java new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/test/jdk/java/net/ServerSocket/MPTCPServer.java @@ -XXX,XX +XXX,XX @@ +/* + * Copyright (c) 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 + * 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. + */ + +import java.io.*; +import java.net.*; + +public class MPTCPServer { + public static void main(String[] args) { + final int PORT = 12345; + + try (ServerSocket serverSocket = new ServerSocket(PORT, 50, null, true)) { + System.out.println("Server started, waiting for client connection ..."); + + Socket clientSocket = serverSocket.accept(); + System.out.println("Client connected: " + clientSocket.getInetAddress()); + + BufferedReader in = new BufferedReader( + new InputStreamReader(clientSocket.getInputStream())); + PrintWriter out = new PrintWriter( + clientSocket.getOutputStream(), true); + + String inputLine; + while ((inputLine = in.readLine()) != null) { + System.out.println("Received from client: " + inputLine); + out.println("Server response: " + inputLine); + + if ("exit".equalsIgnoreCase(inputLine)) { + break; + } + } + + clientSocket.close(); + System.out.println("Connection closed"); + } catch (IOException e) { + System.err.println("Server exception: " + e.getMessage()); + } + } +} diff --git a/test/jdk/java/net/Socket/MPTCPClient.java b/test/jdk/java/net/Socket/MPTCPClient.java new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/test/jdk/java/net/Socket/MPTCPClient.java @@ -XXX,XX +XXX,XX @@ +/* + * Copyright (c) 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 + * 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. + */ + +import java.io.*; +import java.net.*; + +public class MPTCPClient { + public static void main(String[] args) { + final String HOST = "localhost"; + final int PORT = 12345; + + try (Socket socket = new Socket(HOST, PORT, true, true)) { + System.out.println("Connected to server"); + + BufferedReader in = new BufferedReader( + new InputStreamReader(socket.getInputStream())); + PrintWriter out = new PrintWriter( + socket.getOutputStream(), true); + + BufferedReader stdIn = new BufferedReader( + new InputStreamReader(System.in)); + + String userInput; + System.out.println("Enter message (type 'exit' to quit):"); + while ((userInput = stdIn.readLine()) != null) { + out.println(userInput); + + System.out.println("Server response: " + in.readLine()); + + if ("exit".equalsIgnoreCase(userInput)) { + break; + } + } + + System.out.println("Connection closed"); + } catch (UnknownHostException e) { + System.err.println("Host not found: " + HOST); + } catch (IOException e) { + System.err.println("Client I/O error: " + e.getMessage()); + } + } +} -- 2.48.1
From: Geliang Tang <tanggeliang@kylinos.cn> v2: - drop redundant helpers as Matt suggested. - squash into fewer patches. - use "JNI_TRUE" and "(void)mptcp" in Java_sun_nio_ch_Net_socket0 as Xiang Gao suggested. The Multipath TCP (MPTCP) protocol (v1 / RFC 8684) has been added in the upstream Linux kernel since v5.6. See https://mptcp.dev. This patch series introduces comprehensive Multipath TCP (MPTCP) support to the Java Networking API, enabling applications to leverage MPTCP's capabilities for improved reliability and throughput. Geliang Tang (3): Add native MPTCP socket creation support Add MPTCP support to Java Socket/ServerSocket APIs Add test cases for MPTCP socket functionality .../share/classes/java/net/ServerSocket.java | 52 +++++++++++++-- .../share/classes/java/net/Socket.java | 65 +++++++++++++++++-- .../share/classes/java/net/SocketImpl.java | 4 +- .../share/classes/sun/nio/ch/Net.java | 19 ++++-- .../classes/sun/nio/ch/NioSocketImpl.java | 11 +++- src/java.base/unix/native/libnio/ch/Net.c | 14 +++- src/java.base/windows/native/libnio/ch/Net.c | 4 +- .../java/net/ServerSocket/MPTCPServer.java | 58 +++++++++++++++++ test/jdk/java/net/Socket/MPTCPClient.java | 62 ++++++++++++++++++ 9 files changed, 264 insertions(+), 25 deletions(-) create mode 100644 test/jdk/java/net/ServerSocket/MPTCPServer.java create mode 100644 test/jdk/java/net/Socket/MPTCPClient.java -- 2.48.1
This patch introduces native-level support for creating MPTCP sockets by: 1. Extending the socket0 native method in both Unix and Windows implementations to accept an mptcp parameter 2. Adding new overloaded socket() and serverSocket() methods in Net.java that support explicit MPTCP enabling 3. Introducing convenience methods mptcpSocket() and mptcpServerSocket() for easier MPTCP socket creation When MPTCP is enabled and supported by the OS, the socket is created with the IPPROTO_MPTCP protocol for stream sockets. All existing call sites are updated to pass 'false' for the mptcp parameter to maintain backward compatibility. Co-Developed-by: Gang Yan <yangang@kylinos.cn> Signed-off-by: Gang Yan <yangang@kylinos.cn> Co-Developed-by: Xiang Gao <gaoxiang@kylinos.cn> Signed-off-by: Xiang Gao <gaoxiang@kylinos.cn> Signed-off-by: Geliang Tang <geliang@kernel.org> --- .../share/classes/sun/nio/ch/Net.java | 19 ++++++++++++++----- src/java.base/unix/native/libnio/ch/Net.c | 14 ++++++++++++-- src/java.base/windows/native/libnio/ch/Net.c | 4 +++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/java.base/share/classes/sun/nio/ch/Net.java b/src/java.base/share/classes/sun/nio/ch/Net.java index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/sun/nio/ch/Net.java +++ b/src/java.base/share/classes/sun/nio/ch/Net.java @@ -XXX,XX +XXX,XX @@ static FileDescriptor socket() throws IOException { return socket(UNSPEC, true); } - static FileDescriptor socket(ProtocolFamily family, boolean stream) throws IOException { + static FileDescriptor socket(ProtocolFamily family, boolean stream, boolean mptcp) + throws IOException { boolean preferIPv6 = isIPv6Available() && (family != StandardProtocolFamily.INET); - return IOUtil.newFD(socket0(preferIPv6, stream, false, FAST_LOOPBACK)); + return IOUtil.newFD(socket0(preferIPv6, stream, false, FAST_LOOPBACK, mptcp)); + } + + static FileDescriptor socket(ProtocolFamily family, boolean stream) throws IOException { + return socket(family, stream, false); } static FileDescriptor serverSocket() { return serverSocket(UNSPEC); } - static FileDescriptor serverSocket(ProtocolFamily family) { + static FileDescriptor serverSocket(ProtocolFamily family, boolean mptcp) { boolean preferIPv6 = isIPv6Available() && (family != StandardProtocolFamily.INET); - return IOUtil.newFD(socket0(preferIPv6, true, true, FAST_LOOPBACK)); + return IOUtil.newFD(socket0(preferIPv6, true, true, FAST_LOOPBACK, mptcp)); + } + + static FileDescriptor serverSocket(ProtocolFamily family) { + return serverSocket(family, false); } // Due to oddities SO_REUSEADDR on Windows reuse is ignored private static native int socket0(boolean preferIPv6, boolean stream, boolean reuse, - boolean fastLoopback); + boolean fastLoopback, boolean mptcp); public static void bind(FileDescriptor fd, InetAddress addr, int port) throws IOException diff --git a/src/java.base/unix/native/libnio/ch/Net.c b/src/java.base/unix/native/libnio/ch/Net.c index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/unix/native/libnio/ch/Net.c +++ b/src/java.base/unix/native/libnio/ch/Net.c @@ -XXX,XX +XXX,XX @@ Java_sun_nio_ch_Net_canUseIPv6OptionsWithIPv4LocalAddress0(JNIEnv* env, jclass c JNIEXPORT jint JNICALL Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6, - jboolean stream, jboolean reuse, jboolean ignored) + jboolean stream, jboolean reuse, jboolean ignored, + jboolean mptcp) { int fd; int type = (stream ? SOCK_STREAM : SOCK_DGRAM); int domain = (ipv6_available() && preferIPv6) ? AF_INET6 : AF_INET; + int protocol = 0; - fd = socket(domain, type, 0); +#if defined(__linux__) && defined(IPPROTO_MPTCP) + if (stream == JNI_TRUE && mptcp == JNI_TRUE) { + protocol = IPPROTO_MPTCP; + } +#else + (void)mptcp; /* Avoid compile warning when MPTCP is not supported */ +#endif + + fd = socket(domain, type, protocol); if (fd < 0) { return handleSocketError(env, errno); } diff --git a/src/java.base/windows/native/libnio/ch/Net.c b/src/java.base/windows/native/libnio/ch/Net.c index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/windows/native/libnio/ch/Net.c +++ b/src/java.base/windows/native/libnio/ch/Net.c @@ -XXX,XX +XXX,XX @@ Java_sun_nio_ch_Net_canUseIPv6OptionsWithIPv4LocalAddress0(JNIEnv* env, jclass c JNIEXPORT jint JNICALL Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6, - jboolean stream, jboolean reuse, jboolean fastLoopback) + jboolean stream, jboolean reuse, jboolean fastLoopback, + jboolean mptcp) { SOCKET s; int domain = (preferIPv6) ? AF_INET6 : AF_INET; + (void)mptcp; /* not support MPTCP yet */ s = socket(domain, (stream ? SOCK_STREAM : SOCK_DGRAM), 0); if (s != INVALID_SOCKET) { SetHandleInformation((HANDLE)s, HANDLE_FLAG_INHERIT, 0); -- 2.48.1
This patch integrates MPTCP support into the Java socket API by: 1. Adding new constructors in Socket and ServerSocket that accept an mptcp parameter to enable MPTCP explicitly 2. Extending NioSocketImpl to track the MPTCP state and use the appropriate native methods for socket creation 3. Propagating the MPTCP flag through the socket creation chain via new createImpl() factory methods 4. Ensuring backward compatibility by keeping existing constructors unchanged The implementation allows Java applications to use MPTCP seamlessly while maintaining full compatibility with existing TCP-based code. 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/ServerSocket.java | 52 +++++++++++++-- .../share/classes/java/net/Socket.java | 65 +++++++++++++++++-- .../share/classes/java/net/SocketImpl.java | 4 +- .../classes/sun/nio/ch/NioSocketImpl.java | 11 +++- 4 files changed, 115 insertions(+), 17 deletions(-) diff --git a/src/java.base/share/classes/java/net/ServerSocket.java b/src/java.base/share/classes/java/net/ServerSocket.java index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/java/net/ServerSocket.java +++ b/src/java.base/share/classes/java/net/ServerSocket.java @@ -XXX,XX +XXX,XX @@ protected ServerSocket(SocketImpl impl) { * @throws IOException IO error when opening the socket. */ public ServerSocket() throws IOException { - this.impl = createImpl(); + this.impl = createImpl(false); } /** @@ -XXX,XX +XXX,XX @@ public ServerSocket(int port, int backlog) throws IOException { */ @SuppressWarnings("this-escape") public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException { + this(port, backlog, bindAddr, false); + } + + /** + * Create a server using <i>mptcp</i> (Multipath TCP) or TCP protocol with + * the specified port, listen backlog, and local IP address to bind to. + * The <i>bindAddr</i> argument can be used on a multi-homed host for a + * ServerSocket that will only accept connect requests to one of its addresses. + * If <i>bindAddr</i> is null, it will default accepting + * connections on any/all local addresses. + * The port must be between 0 and 65535, inclusive. + * A port number of {@code 0} means that the port number is + * automatically allocated, typically from an ephemeral port range. + * This port number can then be retrieved by calling + * {@link #getLocalPort getLocalPort}. + * + * The {@code backlog} argument is the requested maximum number of + * pending connections on the socket. Its exact semantics are implementation + * specific. In particular, an implementation may impose a maximum length + * or may choose to ignore the parameter altogether. The value provided + * should be greater than {@code 0}. If it is less than or equal to + * {@code 0}, then an implementation specific default will be used. + * + * The {@code mptcp} argument is used to control whether to create a socket + * with MPTCP or TCP protocol. + * + * @param port the port number, or {@code 0} to use a port + * number that is automatically allocated. + * @param backlog requested maximum length of the queue of incoming + * connections. + * @param bindAddr the local InetAddress the server will bind to + * @param mptcp create a socket with MPTCP or TCP protocol. + * + * @throws IOException if an I/O error occurs when opening the socket. + * @throws IllegalArgumentException if the port parameter is outside + * the specified range of valid port values, which is between + * 0 and 65535, inclusive. + */ + @SuppressWarnings("this-escape") + public ServerSocket(int port, int backlog, InetAddress bindAddr, boolean mptcp) throws IOException { if (port < 0 || port > 0xFFFF) throw new IllegalArgumentException("Port value out of range: " + port); if (backlog < 1) backlog = 50; - this.impl = createImpl(); + this.impl = createImpl(mptcp); try { bind(new InetSocketAddress(bindAddr, port), backlog); } catch (IOException e) { @@ -XXX,XX +XXX,XX @@ public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOExcept /** * Create a SocketImpl for a server socket. The SocketImpl is created * without an underlying socket. + * + * @param mptcp create a socket with MPTCP or TCP protocol. */ - private static SocketImpl createImpl() { + private static SocketImpl createImpl(boolean mptcp) { SocketImplFactory factory = ServerSocket.factory; if (factory != null) { return factory.createSocketImpl(); } else { - return SocketImpl.createPlatformSocketImpl(true); + return SocketImpl.createPlatformSocketImpl(true, mptcp); } } @@ -XXX,XX +XXX,XX @@ private SocketImpl platformImplAccept() throws IOException { assert impl instanceof PlatformSocketImpl; // create a new platform SocketImpl and accept the connection - SocketImpl psi = SocketImpl.createPlatformSocketImpl(false); + SocketImpl psi = SocketImpl.createPlatformSocketImpl(false, false); implAccept(psi); return psi; } diff --git a/src/java.base/share/classes/java/net/Socket.java b/src/java.base/share/classes/java/net/Socket.java index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/java/net/Socket.java +++ b/src/java.base/share/classes/java/net/Socket.java @@ -XXX,XX +XXX,XX @@ private static boolean isOutputShutdown(int s) { * @since 1.1 */ public Socket() { - this.impl = createImpl(); + this.impl = createImpl(false); } /** @@ -XXX,XX +XXX,XX @@ public Socket(Proxy proxy) { checkAddress (epoint.getAddress(), "Socket"); } // create a SOCKS or HTTP SocketImpl that delegates to a platform SocketImpl - SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false); + SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false, false); impl = (type == Proxy.Type.SOCKS) ? new SocksSocketImpl(p, delegate) : new HttpConnectSocketImpl(p, delegate, this); } else { @@ -XXX,XX +XXX,XX @@ public Socket(Proxy proxy) { // create a platform or custom SocketImpl for the DIRECT case SocketImplFactory factory = Socket.factory; if (factory == null) { - impl = SocketImpl.createPlatformSocketImpl(false); + impl = SocketImpl.createPlatformSocketImpl(false, false); } else { impl = factory.createSocketImpl(); } @@ -XXX,XX +XXX,XX @@ 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. @@ -XXX,XX +XXX,XX @@ public Socket(InetAddress host, int port, boolean stream) throws IOException { */ private Socket(SocketAddress address, SocketAddress localAddr, boolean stream) throws IOException + { + this(address, localAddr, stream, false); + } + + /** + * 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) { @@ -XXX,XX +XXX,XX @@ private Socket(SocketAddress address, SocketAddress localAddr, boolean stream) assert address instanceof InetSocketAddress; // create the SocketImpl and the underlying socket - SocketImpl impl = createImpl(); + SocketImpl impl = createImpl(mptcp); impl.create(stream); this.impl = impl; @@ -XXX,XX +XXX,XX @@ private Socket(SocketAddress address, SocketAddress localAddr, boolean stream) /** * 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() { + 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); + SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false, mptcp); return new SocksSocketImpl(delegate); } } @@ -XXX,XX +XXX,XX @@ private SocketImpl getImpl() throws SocketException { } SocketImpl impl = this.impl; if (impl == null) { - this.impl = impl = createImpl(); + this.impl = impl = createImpl(false); } try { impl.create(true); diff --git a/src/java.base/share/classes/java/net/SocketImpl.java b/src/java.base/share/classes/java/net/SocketImpl.java index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/java/net/SocketImpl.java +++ b/src/java.base/share/classes/java/net/SocketImpl.java @@ -XXX,XX +XXX,XX @@ public abstract class SocketImpl implements SocketOptions { * Creates an instance of platform's SocketImpl */ @SuppressWarnings("unchecked") - static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(boolean server) { - return (S) new NioSocketImpl(server); + static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(boolean server, boolean mptcp) { + return (S) new NioSocketImpl(server, mptcp); } /** diff --git a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java index XXXXXXX..XXXXXXX 100644 --- a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java @@ -XXX,XX +XXX,XX @@ public final class NioSocketImpl extends SocketImpl implements PlatformSocketImp // true if this is a SocketImpl for a ServerSocket private final boolean server; + // true if this is a SocketImpl for a MptcpServerSocket + private final boolean mptcp; + // Lock held when reading (also used when accepting or connecting) private final ReentrantLock readLock = new ReentrantLock(); @@ -XXX,XX +XXX,XX @@ public final class NioSocketImpl extends SocketImpl implements PlatformSocketImp /** * Creates an instance of this SocketImpl. * @param server true if this is a SocketImpl for a ServerSocket + * @param mptcp enable MPTCP */ - public NioSocketImpl(boolean server) { + public NioSocketImpl(boolean server, boolean mptcp) { this.server = server; + this.mptcp = mptcp; } /** @@ -XXX,XX +XXX,XX @@ protected void create(boolean stream) throws IOException { throw new IOException("Already created"); FileDescriptor fd; if (server) { - fd = Net.serverSocket(); + fd = Net.serverSocket(Net.UNSPEC, mptcp); } else { - fd = Net.socket(); + fd = Net.socket(Net.UNSPEC, true, mptcp); } Runnable closer = closerFor(fd); this.fd = fd; -- 2.48.1
This patch adds comprehensive test coverage for the MPTCP socket implementation by introducing two test programs: 1. MPTCPServer.java - an echo server that uses MPTCP-enabled ServerSocket 2. MPTCPClient.java - a client that connects to the server using MPTCP Socket The tests verify: - MPTCP socket creation and connection establishment - Bidirectional communication over MPTCP subflows - Proper stream handling and error conditions - Graceful shutdown and resource cleanup These tests demonstrate the usage of the new MPTCP-enabled constructors and provide a functional validation of the MPTCP implementation. Co-Developed-by: Gang Yan <yangang@kylinos.cn> Signed-off-by: Gang Yan <yangang@kylinos.cn> Signed-off-by: Geliang Tang <geliang@kernel.org> --- .../java/net/ServerSocket/MPTCPServer.java | 58 +++++++++++++++++ test/jdk/java/net/Socket/MPTCPClient.java | 62 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 test/jdk/java/net/ServerSocket/MPTCPServer.java create mode 100644 test/jdk/java/net/Socket/MPTCPClient.java diff --git a/test/jdk/java/net/ServerSocket/MPTCPServer.java b/test/jdk/java/net/ServerSocket/MPTCPServer.java new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/test/jdk/java/net/ServerSocket/MPTCPServer.java @@ -XXX,XX +XXX,XX @@ +/* + * Copyright (c) 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 + * 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. + */ + +import java.io.*; +import java.net.*; + +public class MPTCPServer { + public static void main(String[] args) { + final int PORT = 12345; + + try (ServerSocket serverSocket = new ServerSocket(PORT, 50, null, true)) { + System.out.println("Server started, waiting for client connection ..."); + + Socket clientSocket = serverSocket.accept(); + System.out.println("Client connected: " + clientSocket.getInetAddress()); + + BufferedReader in = new BufferedReader( + new InputStreamReader(clientSocket.getInputStream())); + PrintWriter out = new PrintWriter( + clientSocket.getOutputStream(), true); + + String inputLine; + while ((inputLine = in.readLine()) != null) { + System.out.println("Received from client: " + inputLine); + out.println("Server response: " + inputLine); + + if ("exit".equalsIgnoreCase(inputLine)) { + break; + } + } + + clientSocket.close(); + System.out.println("Connection closed"); + } catch (IOException e) { + System.err.println("Server exception: " + e.getMessage()); + } + } +} diff --git a/test/jdk/java/net/Socket/MPTCPClient.java b/test/jdk/java/net/Socket/MPTCPClient.java new file mode 100644 index XXXXXXX..XXXXXXX --- /dev/null +++ b/test/jdk/java/net/Socket/MPTCPClient.java @@ -XXX,XX +XXX,XX @@ +/* + * Copyright (c) 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 + * 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. + */ + +import java.io.*; +import java.net.*; + +public class MPTCPClient { + public static void main(String[] args) { + final String HOST = "localhost"; + final int PORT = 12345; + + try (Socket socket = new Socket(HOST, PORT, true, true)) { + System.out.println("Connected to server"); + + BufferedReader in = new BufferedReader( + new InputStreamReader(socket.getInputStream())); + PrintWriter out = new PrintWriter( + socket.getOutputStream(), true); + + BufferedReader stdIn = new BufferedReader( + new InputStreamReader(System.in)); + + String userInput; + System.out.println("Enter message (type 'exit' to quit):"); + while ((userInput = stdIn.readLine()) != null) { + out.println(userInput); + + System.out.println("Server response: " + in.readLine()); + + if ("exit".equalsIgnoreCase(userInput)) { + break; + } + } + + System.out.println("Connection closed"); + } catch (UnknownHostException e) { + System.err.println("Host not found: " + HOST); + } catch (IOException e) { + System.err.println("Client I/O error: " + e.getMessage()); + } + } +} -- 2.48.1