Listens for incoming TCP connection requests.
The TcpListener class provides a simple way to listen for incoming TCP connection requests.
It can bind to a specific IP address and port, or to all network interfaces on a machine.
Once started, it listens for incoming connection requests and can accept them using the
AcceptTcpClient or AcceptSocket methods.
This class is useful for creating server applications that need to accept network connections, such as web servers, chat applications, or custom network services.
public class TcpListener : IDisposable
System.ObjectSystem.Net.Sockets.TcpListenerSystem.IDisposableThe following example demonstrates how to create a simple TCP server that listens on port 8080.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SimpleTcpServer
{
public static void StartListening()
{
// Set the TcpListener on port 8080.
int port = 8080;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
TcpListener server = new TcpListener(localAddr, port);
// Start listening for connections.
server.Start();
Console.WriteLine($"Server started. Listening on port {port}...");
// Program is always listening.
while (true)
{
Console.WriteLine("Waiting for a connection...");
// Program blocks while waiting for an incoming connection.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
NetworkStream stream = client.GetStream();
// Read and echo data.
byte[] bytes = new byte[1024];
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate bytes of the network message into a string.
string data = Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine($"Received: {data}");
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine($"Sent: {data}");
}
// Shutdown and end the connection.
client.Close();
}
}
public static void Main(string[] args)
{
StartListening();
}
}
| Signature | Description |
|---|---|
TcpListener(IPAddress localEP)
|
Initializes a new instance of the TcpListener class,
specifying the local endpoint. |
TcpListener(IPAddress localEP, int port)
|
Initializes a new instance of the TcpListener class,
specifying the local endpoint and port. |
TcpListener(int port)
|
Initializes a new instance of the TcpListener class,
specifying the port to listen on. |
| Signature | Description |
|---|---|
AcceptTcpClient()
|
Accepts an incoming TcpClient connection. |
AcceptTcpClientAsync()
|
Asynchronously accepts an incoming connection. |
AcceptSocket()
|
Accepts an incoming Socket connection. |
AcceptSocketAsync()
|
Asynchronously accepts an incoming connection. |
Start()
|
Starts listening for incoming connection requests. |
Stop()
|
Stops listening for incoming TCP connection requests. |
Equals(Object obj)
|
Determines whether the specified object instances are equal. |
GetHashCode()
|
Returns the hash code for the current instance. |
GetType()
|
Gets the Type of the current instance. |
ToString()
|
Returns a string that represents the current object. |
| Signature | Description |
|---|---|
Active : bool
|
Gets a value indicating whether the TcpListener is active. |
LocalEndpoint : EndPoint
|
Gets the local endpoint for the TcpListener. |
Server : Socket
|
Gets the underlying Socket. |
This class does not define any events.
This class does not define any fields.