Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TcpListener should implement IDisposable #88043

Merged
merged 8 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand All @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,6 +167,11 @@ public void Stop()
_serverSocket = null;
}

/// <summary>
/// Releases all resources used by the current <see cref="TcpListener"/> instance.
/// </summary>
public void Dispose() => Stop();

// Determine if there are pending connection requests.
public bool Pending()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
AlexRadch marked this conversation as resolved.
Show resolved Hide resolved
listener.Dispose();
Assert.False(listener.Active);
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public void AllowNatTraversal_NotStarted_SetSuccessfully()
Expand Down