TcpListener.LocalEndpoint 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取当前 EndPoint 的基础 TcpListener。
public:
property System::Net::EndPoint ^ LocalEndpoint { System::Net::EndPoint ^ get(); };
public System.Net.EndPoint LocalEndpoint { get; }
member this.LocalEndpoint : System.Net.EndPoint
Public ReadOnly Property LocalEndpoint As EndPoint
属性值
示例
下面的代码示例显示 侦听传入连接请求的本地 IP 地址和端口号 TcpListener 。
try
{
// Use the Pending method to poll the underlying socket instance for client connection requests.
TcpListener^ tcpListener = gcnew TcpListener( portNumber );
tcpListener->Start();
if ( !tcpListener->Pending() )
{
Console::WriteLine( "Sorry, no connection requests have arrived" );
}
else
{
//Accept the pending client connection and return a TcpClient object^ initialized for communication.
TcpClient^ tcpClient = tcpListener->AcceptTcpClient();
// Using the RemoteEndPoint property.
Console::WriteLine( "I am listening for connections on {0} on port number {1}",
IPAddress::Parse( ( (IPEndPoint^)(tcpListener->LocalEndpoint) )->Address->ToString() ),
( (IPEndPoint^)(tcpListener->LocalEndpoint) )->Port );
const int portNumber = 13;
try
{
// Use the Pending method to poll the underlying socket instance for client connection requests.
IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
TcpListener tcpListener = new TcpListener(ipAddress, portNumber);
tcpListener.Start();
if (!tcpListener.Pending())
{
Console.WriteLine("Sorry, no connection requests have arrived");
}
else
{
//Accept the pending client connection and return a TcpClient object initialized for communication.
TcpClient tcpClient = tcpListener.AcceptTcpClient();
// Using the RemoteEndPoint property.
Console.WriteLine("I am listening for connections on " +
IPAddress.Parse(((IPEndPoint)tcpListener.LocalEndpoint).Address.ToString()) +
"on port number " + ((IPEndPoint)tcpListener.LocalEndpoint).Port.ToString());
//Close the tcpListener and tcpClient instances
tcpClient.Close();
}
tcpListener.Stop();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Try
Dim ipAddress As IPAddress = Dns.Resolve("localhost").AddressList(0)
Dim tcpListener As New TcpListener(ipAddress, portNumber)
tcpListener.Start()
' Use the Pending method to poll the underlying socket instance for client connection requests.
If Not tcpListener.Pending() Then
Console.WriteLine("Sorry, no connection requests have arrived")
Else
'Accept the pending client connection and return a TcpClient object initialized for communication.
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
' Using the RemoteEndPoint property.
Console.Write("I am listening for connections on ")
Console.Writeline(IPAddress.Parse(CType(tcpListener.LocalEndpoint, IPEndPoint).Address.ToString()))
Console.Write("on port number ")
Console.Write(CType(tcpListener.LocalEndpoint, IPEndPoint).Port.ToString())
注解
在 LocalEndpoint 建立套接字连接后,可以使用 属性标识用于侦听传入客户端连接请求的本地网络接口和端口号。 必须先将此EndPointIPEndPoint强制转换为 。 然后, IPEndPoint.Address 可以调用 属性来检索本地 IP 地址,并 IPEndPoint.Port 调用 属性来检索本地端口号。