Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -726,6 +726,7 @@ public void AllowNatTraversal(bool allowed) { }
public void Start() { }
public void Start(int backlog) { }
public void Stop() { }
public void Dispose() { }
}
[System.FlagsAttribute]
public enum TransmitFileOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
<Compile Include="System\Net\Sockets\SocketOptionLevel.cs" />
<Compile Include="System\Net\Sockets\SocketOptionName.cs" />
<Compile Include="System\Net\Sockets\SocketShutdown.cs" />
<Compile Include="System\Net\Sockets\TCPClient.cs" />
<Compile Include="System\Net\Sockets\TCPListener.cs" />
<Compile Include="System\Net\Sockets\TcpClient.cs" />
<Compile Include="System\Net\Sockets\TcpListener.cs" />
<Compile Include="System\Net\Sockets\TransmitFileOptions.cs" />
<Compile Include="System\Net\Sockets\UDPClient.cs" />
<Compile Include="System\Net\Sockets\UdpReceiveResult.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
using System.Threading.Tasks;
using System.Runtime.Versioning;
using System.Diagnostics;
using System.ComponentModel;

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 @@ -159,14 +160,22 @@ public void Start(int backlog)
_active = true;
}

// Closes the network connection.
/// <summary>
/// Closes the network connection.
/// </summary>
public void Stop()
{
_serverSocket?.Dispose();
_active = false;
_serverSocket = null;
}

/// <summary>
/// Closes the network connection.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
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,28 @@ 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 = new DerivedTcpListener(IPAddress.Loopback, 0);
using ((IDisposable)listener)
{
Assert.False(listener.Active);
listener.Start();
Assert.True(listener.Active);
}
Assert.False(listener.Active);
}

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