请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
TcpListener 类
 LocalEndpoint 属性
全部折叠/全部展开 全部折叠
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
TcpListener.LocalEndpoint 属性

获取当前 TcpListener 的基础 EndPoint

命名空间:System.Net.Sockets
程序集:System(在 system.dll 中)

Visual Basic(声明)
Public ReadOnly Property LocalEndpoint As EndPoint
Visual Basic(用法)
Dim instance As TcpListener
Dim value As EndPoint

value = instance.LocalEndpoint
C#
public EndPoint LocalEndpoint { get; }
C++
public:
property EndPoint^ LocalEndpoint {
    EndPoint^ get ();
}
J#
/** @property */
public EndPoint get_LocalEndpoint ()
JScript
public function get LocalEndpoint () : EndPoint

属性值

Socket 绑定到的 EndPoint

建立套接字连接后,可使用 LocalEndpoint 属性来标识正用于侦听传入客户端连接请求的本地网络接口和端口号。必须首先将该 EndPoint 强制转换为 IPEndPoint。然后,可以调用 IPEndPoint.Address 属性来检索本地 IP 地址,调用 IPEndPoint.Port 属性来检索本地端口号。

下面的代码示例显示 TcpListener 在其上侦听传入连接请求的本地 IP 地址和端口号。

Visual Basic
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())
      
C#
        
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());
C++
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 );
J#
try {
    // Use the Pending method to poll the underlying socket instance
    // for client connection requests.
    IPAddress ipAddress = Dns.Resolve("localhost").get_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.get_LocalEndpoint())).get_Address().ToString())
            + "on port number " + ((Int32)((IPEndPoint)
            (tcpListener.get_LocalEndpoint())).get_Port()).ToString());

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利 | 商标 | 隐私权声明
Page view tracker