public class TcpListener
|
You can create an instance of the TcpListener class by providing one of the following constructor argument lists:
Use the TcpListener.Start property to begin listening for incoming connection requests. Employ the TcpListener.Pending method to determine if connections are pending. Use TcpListener.AcceptSocket to retrieve a Socket, or TcpListener.AcceptTcpClient to retrieve a TcpClient used for facilitating communication with the remote machine. Finally, use TcpListener.Stop to close the underlying listening Socket.
const int portNumber = 13; TcpListener tcpListener = new TcpListener(portNumber); tcpListener.Start(); Console.WriteLine("Waiting for a connection...."); try{ //Accept the pending client connection and return a TcpClient initialized for communication. TcpClient tcpClient = tcpListener.AcceptTcpClient(); Console.WriteLine("Connection accepted."); NetworkStream networkStream = tcpClient.GetStream(); string responseString = "You have successfully connected to me"; Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString); networkStream.Write(sendBytes, 0, sendBytes.Length); Console.WriteLine("Message Sent /> : " + responseString); //Any communication with the remote client using the TcpClient can go here. // //////// //Close TcpListener and TcpClient. tcpClient.Close(); tcpListener.Stop(); } catch (Exception e) { Console.WriteLine(e.ToString()); }
ctor #1 | Overloaded:.ctor(int port) Initializes a new instance of the TcpListener class that listens on the specified port. |
ctor #2 | Overloaded:.ctor(IPEndPoint localEP) Initializes a new instance of the TcpListener class with the specified local endpoint. |
ctor #3 | Overloaded:.ctor(IPAddress localaddr, int port) Initializes a new instance of the TcpListener class that listens to the specified IP address and port. |
LocalEndpoint | Read-only Gets the underlying EndPoint of the current TcpListener. |
AcceptSocket | Accepts a pending connection request. |
AcceptTcpClient | Accepts a pending connection request |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Pending | Determines if there are pending connection requests. |
Start | Starts listening to network requests. |
Stop | Closes the listener. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Active | Read-only Gets a value that indicates whether TcpListener is actively listening for client connections. |
Server | Read-only Gets the underlying network Socket. |
Finalize | Overridden: Frees resources used by the TcpClient class. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public TcpListener( |
port
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The port parameter is not between IPEndPoint.MinPort and IPEndPoint.MaxPort. |
//Creates an instance of the TcpListener class by providing a local port number. try{ TcpListener tcpListener = new TcpListener(13); } catch ( Exception e ){ Console.WriteLine( e.ToString()); }
public TcpListener( |
localEP
Exception Type | Condition |
---|---|
ArgumentNullException | The localEP parameter is null. |
//Creates an instance of the TcpListener class by providing a local endpoint. IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0]; IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000); try{ TcpListener tcpListener = new TcpListener(ipLocalEndPoint); } catch ( Exception e ){ Console.WriteLine( e.ToString()); }
localaddr
port
Exception Type | Condition |
---|---|
ArgumentNullException | The localaddr parameter is null. |
ArgumentOutOfRangeException | The port parameter is not between IPEndPoint.MinPort and IPEndPoint.MaxPort. |
//Creates an instance of the TcpListener class by providing a local IP address and port number. IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0]; try{ TcpListener tcpListener = new TcpListener(ipAddress, 13); } catch ( Exception e){ Console.WriteLine( e.ToString()); }
protected bool Active {get;}
|
public EndPoint LocalEndpoint {get;}
|
protected Socket Server {get;}
|
public Socket AcceptSocket(); |
Exception Type | Condition |
---|---|
InvalidOperationException | The listener has not been started with a call to TcpListener.Start. |
// Accepts the pending client connection and returns a socket for communciation. Socket socket = tcpListener.AcceptSocket(); Console.WriteLine("Connection accepted."); string responseString = "You have successfully connected to me"; //Forms and sends a response string to the connected client. Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString); int i = socket.Send(sendBytes); Console.WriteLine("Message Sent /> : " + responseString);
public TcpClient AcceptTcpClient(); |
Exception Type | Condition |
---|---|
InvalidOperationException | The listener has not been started with a call to TcpListener.Start. |
// Accepts the pending client connection and returns a socket for communciation. Socket socket = tcpListener.AcceptSocket(); Console.WriteLine("Connection accepted."); string responseString = "You have successfully connected to me"; //Forms and sends a response string to the connected client. Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString); int i = socket.Send(sendBytes); Console.WriteLine("Message Sent /> : " + responseString);
~TcpListener(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public bool Pending(); |
Exception Type | Condition |
---|---|
InvalidOperationException | The listener has not been started with a call to TcpListener.Start. |
public void Start(); |
Exception Type | Condition |
---|---|
SocketException | An error occurs while opening the network socket. |
This method only listens for connection requests. To detect these requests, you can use one of the following:
const int portNumber = 13; TcpListener tcpListener = new TcpListener(portNumber); tcpListener.Start(); Console.WriteLine("Waiting for a connection...."); try{ //Accept the pending client connection and return a TcpClient initialized for communication. TcpClient tcpClient = tcpListener.AcceptTcpClient(); Console.WriteLine("Connection accepted."); NetworkStream networkStream = tcpClient.GetStream(); string responseString = "You have successfully connected to me"; Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString); networkStream.Write(sendBytes, 0, sendBytes.Length); Console.WriteLine("Message Sent /> : " + responseString); //Any communication with the remote client using the TcpClient can go here. // //////// //Close TcpListener and TcpClient. tcpClient.Close(); tcpListener.Stop(); } catch (Exception e) { Console.WriteLine(e.ToString()); }
public void Stop(); |
Exception Type | Condition |
---|---|
SocketException | An error occurs while closing the network socket. |
const int portNumber = 13; TcpListener tcpListener = new TcpListener(portNumber); tcpListener.Start(); Console.WriteLine("Waiting for a connection...."); try{ //Accept the pending client connection and return a TcpClient initialized for communication. TcpClient tcpClient = tcpListener.AcceptTcpClient(); Console.WriteLine("Connection accepted."); NetworkStream networkStream = tcpClient.GetStream(); string responseString = "You have successfully connected to me"; Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString); networkStream.Write(sendBytes, 0, sendBytes.Length); Console.WriteLine("Message Sent /> : " + responseString); //Any communication with the remote client using the TcpClient can go here. // //////// //Close TcpListener and TcpClient. tcpClient.Close(); tcpListener.Stop(); } catch (Exception e) { Console.WriteLine(e.ToString()); }
public virtual string ToString(); |