Skip to content

Commit 475c9ac

Browse files
TcpListener should implement IDisposable (#88043)
* TcpListener should implement IDisposable * lint * Code review * Update src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs Co-authored-by: Anton Firszov <antonfir@gmail.com> * Update TCPListener.cs [EditorBrowsable(EditorBrowsableState.Never)] * Code review * lint * Code review --------- Co-authored-by: Anton Firszov <antonfir@gmail.com>
1 parent 5a968d5 commit 475c9ac

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ public void EndConnect(System.IAsyncResult asyncResult) { }
699699
~TcpClient() { }
700700
public System.Net.Sockets.NetworkStream GetStream() { throw null; }
701701
}
702-
public partial class TcpListener
702+
public partial class TcpListener : System.IDisposable
703703
{
704704
[System.ObsoleteAttribute("This constructor has been deprecated. Use TcpListener(IPAddress localaddr, int port) instead.")]
705705
public TcpListener(int port) { }
@@ -720,6 +720,7 @@ public void AllowNatTraversal(bool allowed) { }
720720
public System.IAsyncResult BeginAcceptSocket(System.AsyncCallback? callback, object? state) { throw null; }
721721
public System.IAsyncResult BeginAcceptTcpClient(System.AsyncCallback? callback, object? state) { throw null; }
722722
public static System.Net.Sockets.TcpListener Create(int port) { throw null; }
723+
public void Dispose() { }
723724
public System.Net.Sockets.Socket EndAcceptSocket(System.IAsyncResult asyncResult) { throw null; }
724725
public System.Net.Sockets.TcpClient EndAcceptTcpClient(System.IAsyncResult asyncResult) { throw null; }
725726
public bool Pending() { throw null; }

src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace System.Net.Sockets
1111
// The System.Net.Sockets.TcpListener class provide TCP services at a higher level of abstraction
1212
// than the System.Net.Sockets.Socket class. System.Net.Sockets.TcpListener is used to create a
1313
// host process that listens for connections from TCP clients.
14-
public class TcpListener
14+
public class TcpListener : IDisposable
1515
{
1616
private readonly IPEndPoint _serverSocketEP;
1717
private Socket? _serverSocket;
@@ -167,6 +167,11 @@ public void Stop()
167167
_serverSocket = null;
168168
}
169169

170+
/// <summary>
171+
/// Releases all resources used by the current <see cref="TcpListener"/> instance.
172+
/// </summary>
173+
public void Dispose() => Stop();
174+
170175
// Determine if there are pending connection requests.
171176
public bool Pending()
172177
{

src/libraries/System.Net.Sockets/tests/FunctionalTests/TcpListenerTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ public void Active_TrueWhileRunning(int ctor)
4343
Assert.False(listener.Active);
4444
}
4545

46+
[Fact]
47+
public void IDisposable_DisposeWorksAsStop()
48+
{
49+
var listener = new DerivedTcpListener(IPAddress.Loopback, 0);
50+
using (listener)
51+
{
52+
Assert.False(listener.Active);
53+
listener.Start();
54+
Assert.True(listener.Active);
55+
}
56+
Assert.False(listener.Active);
57+
listener.Dispose();
58+
Assert.False(listener.Active);
59+
}
60+
4661
[Fact]
4762
[PlatformSpecific(TestPlatforms.Windows)]
4863
public void AllowNatTraversal_NotStarted_SetSuccessfully()

0 commit comments

Comments
 (0)