System.Net.Sockets.TcpListener Class

Listens for connections from TCP network clients.

See Also: TcpListener Members

Syntax

public class TcpListener

Remarks

The System.Net.Sockets.TcpListener class provides simple methods that listen for and accept incoming connection requests in blocking synchronous mode. You can use either a System.Net.Sockets.TcpClient or a System.Net.Sockets.Socket to connect with a System.Net.Sockets.TcpListener. Create a System.Net.Sockets.TcpListener using an System.Net.IPEndPoint, a Local IP address and port number, or just a port number. Specify System.Net.IPAddress.Any for the local IP address and 0 for the local port number if you want the underlying service provider to assign those values for you. If you choose to do this, you can use the TcpListener.LocalEndpoint property to identify the assigned information, after the socket has connected.

Use the TcpListener.Start method to begin listening for incoming connection requests. TcpListener.Start will queue incoming connections until you either call the TcpListener.Stop method or it has queued SocketOptionName.MaxConnections. Use either TcpListener.AcceptSocket or TcpListener.AcceptTcpClient to pull a connection from the incoming connection request queue. These two methods will block. If you want to avoid blocking, you can use the TcpListener.Pending method first to determine if connection requests are available in the queue.

Call the TcpListener.Stop method to close the System.Net.Sockets.TcpListener.

Note:

The TcpListener.Stop method does not close any accepted connections. You are responsible for closing these separately.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.3300.0, 1.0.5000.0, 2.0.0.0, 4.0.0.0