Upraviť

Zdieľať cez


TcpListener.Server Property

Definition

Gets the underlying network Socket.

public:
 property System::Net::Sockets::Socket ^ Server { System::Net::Sockets::Socket ^ get(); };
protected:
 property System::Net::Sockets::Socket ^ Server { System::Net::Sockets::Socket ^ get(); };
public System.Net.Sockets.Socket Server { get; }
protected System.Net.Sockets.Socket Server { get; }
member this.Server : System.Net.Sockets.Socket
Public ReadOnly Property Server As Socket
Protected ReadOnly Property Server As Socket

Property Value

The underlying Socket.

Examples

The following code example demonstrates the use of the Server property. The underlying Socket is retrieved and the LingerSocket option is configured to time out after 10 seconds if data still remains in the network buffer after the connection is closed.

public static void listenerOption(string host, int port)
{
    IPHostEntry server = Dns.Resolve(host);
    IPAddress ipAddress = server.AddressList[0];

    Console.WriteLine("listening on {0}, port {1}", ipAddress, port);
    TcpListener listener = new TcpListener(ipAddress, port);
    Socket listenerSocket = listener.Server;		

    LingerOption lingerOption = new LingerOption(true, 10);
    listenerSocket.SetSocketOption(SocketOptionLevel.Socket, 
                      SocketOptionName.Linger, 
                      lingerOption);

    // start listening and process connections here.
    listener.Start();
}
Public Shared Sub listenerOption(host As String, port As Integer)
   Dim server As IPHostEntry = Dns.Resolve(host)
   Dim ipAddress As IPAddress = server.AddressList(0)
   
   Console.WriteLine("listening on {0}, port {1}", ipAddress, port)
   Dim listener As New TcpListener(ipAddress, port)
   Dim listenerSocket As Socket = listener.Server
   
   Dim lingerOption As New LingerOption(True, 10)
   listenerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption)
   
   ' start listening and process connections here.
   listener.Start()
End Sub

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

The Server property 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.

Applies to

See also