Export (0) Print
Expand All

TcpListener.Active Property

Gets a value that indicates whether TcpListener is actively listening for client connections.

[Visual Basic]
Protected ReadOnly Property Active As Boolean
[C#]
protected bool Active {get;}
[C++]
protected: __property bool get_Active();
[JScript]
protected function get Active() : Boolean;

Property Value

true if TcpListener is actively listening; otherwise false.

Remarks

Classes deriving from TcpListener can use this property to determine if the Socket is currently listening for incoming connection attempts. The Active property can be used to avoid redundant Start attempts.

Example

[Visual Basic, C#, C++] The following class derives from TcpListener and implements the protected Active method. A check is made to see if the TcpListener has processed a connection before setting Socket options.

[Visual Basic] 
Imports System
Imports System.Net
Imports System.Net.Sockets

Public Class MyTcpListenerDerivedClass
   Inherits TcpListener
   
   Public Sub New(address As IPAddress, port As Integer)
      MyBase.New(address, port)
   End Sub 'New
   
   Public Sub UsingProtectedMethods()
      ' The protected property 'Active' determines whether a connection is established.
      If Me.Active Then
         Dim s As Socket = Me.Server
         'Limit socket linger time to 10 seconds.
         Dim lingerOption As New LingerOption(True, 10)
         s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption)
      End If
   End Sub 
End Class 


[C#] 
using System;
using System.Net; 
using System.Net.Sockets;

public class MyTcpListenerDerivedClass : TcpListener
{
    public MyTcpListenerDerivedClass(IPAddress address, int port) : base(address, port){}
    public  void  UsingProtectedMethods()
    {
        // The protected property 'Active' determines whether a connection is established.
        if (this.Active)
        {
            Socket s = this.Server;
            // Limit socket linger time to 10 seconds.
            LingerOption lingerOption = new LingerOption(true, 10);
            s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
        }
    }
}


[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Net; 
using namespace System::Net::Sockets;

__gc class MyTcpListenerDerivedClass : public TcpListener {
public:
    MyTcpListenerDerivedClass(int port) : TcpListener(port) {}

    void UsingProtectedMethods() {
        // The protected property 'Active' determines whether a connection is established.
        if (this->Active) 
        {
            Socket* s = this->Server;
            // Limit socket linger time to 10 seconds.
            LingerOption* lingerOption = new LingerOption(true, 10);
            s->SetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Linger, lingerOption);
        }
    }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

TcpListener Class | TcpListener Members | System.Net.Sockets Namespace | Start

Show:
© 2014 Microsoft