diff --git a/src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs b/src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs index e60d1d4565334..c9b67c966f7a0 100644 --- a/src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs +++ b/src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs @@ -699,7 +699,7 @@ public void EndConnect(System.IAsyncResult asyncResult) { } ~TcpClient() { } public System.Net.Sockets.NetworkStream GetStream() { throw null; } } - public partial class TcpListener + public partial class TcpListener : System.IDisposable { [System.ObsoleteAttribute("This constructor has been deprecated. Use TcpListener(IPAddress localaddr, int port) instead.")] public TcpListener(int port) { } @@ -720,6 +720,7 @@ public void AllowNatTraversal(bool allowed) { } public System.IAsyncResult BeginAcceptSocket(System.AsyncCallback? callback, object? state) { throw null; } public System.IAsyncResult BeginAcceptTcpClient(System.AsyncCallback? callback, object? state) { throw null; } public static System.Net.Sockets.TcpListener Create(int port) { throw null; } + public void Dispose() { } public System.Net.Sockets.Socket EndAcceptSocket(System.IAsyncResult asyncResult) { throw null; } public System.Net.Sockets.TcpClient EndAcceptTcpClient(System.IAsyncResult asyncResult) { throw null; } public bool Pending() { throw null; } diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs index 4cb923fb0abf6..39b931cc63472 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs @@ -11,7 +11,7 @@ namespace System.Net.Sockets // The System.Net.Sockets.TcpListener class provide TCP services at a higher level of abstraction // than the System.Net.Sockets.Socket class. System.Net.Sockets.TcpListener is used to create a // host process that listens for connections from TCP clients. - public class TcpListener + public class TcpListener : IDisposable { private readonly IPEndPoint _serverSocketEP; private Socket? _serverSocket; @@ -167,6 +167,11 @@ public void Stop() _serverSocket = null; } + /// + /// Releases all resources used by the current instance. + /// + public void Dispose() => Stop(); + // Determine if there are pending connection requests. public bool Pending() { diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/TcpListenerTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/TcpListenerTest.cs index 9415b4961a251..2e47f057a8d8c 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/TcpListenerTest.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/TcpListenerTest.cs @@ -43,6 +43,21 @@ public void Active_TrueWhileRunning(int ctor) Assert.False(listener.Active); } + [Fact] + public void IDisposable_DisposeWorksAsStop() + { + var listener = new DerivedTcpListener(IPAddress.Loopback, 0); + using (listener) + { + Assert.False(listener.Active); + listener.Start(); + Assert.True(listener.Active); + } + Assert.False(listener.Active); + listener.Dispose(); + Assert.False(listener.Active); + } + [Fact] [PlatformSpecific(TestPlatforms.Windows)] public void AllowNatTraversal_NotStarted_SetSuccessfully()