File tree Expand file tree Collapse file tree 3 files changed +23
-2
lines changed
src/libraries/System.Net.Sockets Expand file tree Collapse file tree 3 files changed +23
-2
lines changed Original file line number Diff line number Diff 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 ; }
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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 ( )
You can’t perform that action at this time.
0 commit comments