Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TcpListener.Server Property

Gets the underlying network Socket.

[Visual Basic]
Protected ReadOnly Property Server As Socket
[C#]
protected Socket Server {get;}
[C++]
protected: __property Socket* get_Server();
[JScript]
protected function get Server() : Socket;

Property Value

The underlying Socket.

Remarks

TcpListener creates a Socket to listen for incoming client connection requests. Classes deriving from TcpListener can use this property to get this Socket. Use the underlying Socket returned by the Server property if you require access beyond that which TcpListener provides.

Note    Server only returns the Socket used to listen for incoming client connection requests. Use the AcceptSocket method to accept a pending connection request and obtain a Socket for sending and receiving data. You can also use the AcceptTcpClient method to accept a pending connection request and obtain a TcpClient for sending and receiving data.

Example

[Visual Basic, C#, C++] The following class derives from TcpListener and implements the Server method. The underlying Socket is retrieved and the Linger Socket option is configured to time-out after 10 seconds if data still remains in the network buffer after the connection is closed.

[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 | Socket | AcceptSocket | AcceptTcpClient

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft