Socket.LocalEndPoint Socket.LocalEndPoint Socket.LocalEndPoint Socket.LocalEndPoint Property

定义

获取本地终结点。Gets the local endpoint.

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

属性值

EndPoint 当前用以进行通信的 SocketThe EndPoint that the Socket is using for communications.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket.

示例

下面的代码示例检索并显示本地和远程终结点。The following code example retrieves and displays the local and remote endpoints.

s->Connect( lep );

// Uses the RemoteEndPoint property.
Console::WriteLine(  "I am connected to {0} on port number {1}",
   IPAddress::Parse( ( ( (IPEndPoint^)(s->RemoteEndPoint) )->Address)->ToString() ),
   ( (IPEndPoint^)(s->RemoteEndPoint) )->Port.ToString() );

// Uses the LocalEndPoint property.
Console::Write(  "My local IpAddress is : {0}\nI am connected on port number {1}",
   IPAddress::Parse( ( ( (IPEndPoint^)(s->LocalEndPoint) )->Address)->ToString() ),
   ( (IPEndPoint^)(s->LocalEndPoint) )->Port.ToString() );
s.Connect (lep);

// Using the RemoteEndPoint property.
Console.WriteLine ("I am connected to " + IPAddress.Parse (((IPEndPoint)s.RemoteEndPoint).Address.ToString ()) + "on port number " + ((IPEndPoint)s.RemoteEndPoint).Port.ToString ());

// Using the LocalEndPoint property.
Console.WriteLine ("My local IpAddress is :" + IPAddress.Parse (((IPEndPoint)s.LocalEndPoint).Address.ToString ()) + "I am connected on port number " + ((IPEndPoint)s.LocalEndPoint).Port.ToString ());

s.Connect(lep)

' Using the RemoteEndPoint property.
Console.WriteLine("I am connected to ")
Console.WriteLine(IPAddress.Parse(CType(s.RemoteEndPoint, IPEndPoint).Address.ToString()))
Console.WriteLine("on port number ")
Console.WriteLine(CType(s.RemoteEndPoint, IPEndPoint).Port.ToString())

' Using the LocalEndPoint property.
Console.WriteLine("My local IpAddress is :")
Console.WriteLine(IPAddress.Parse(CType(s.LocalEndPoint, IPEndPoint).Address.ToString()))
Console.WriteLine("I am connected on port number ")
Console.WriteLine(CType(s.LocalEndPoint, IPEndPoint).Port.ToString())

注解

LocalEndPoint属性获取EndPoint,其中包含到其中的本地 IP 地址和端口号在Socket绑定。The LocalEndPoint property gets an EndPoint that contains the local IP address and port number to which your Socket is bound. 您必须将此对象转换EndPointIPEndPoint之前检索的任何信息。You must cast this EndPoint to an IPEndPoint before retrieving any information. 然后,可以调用IPEndPoint.Address方法来检索本地IPAddress,和IPEndPoint.Port方法来检索本地端口号。You can then call the IPEndPoint.Address method to retrieve the local IPAddress, and the IPEndPoint.Port method to retrieve the local port number.

LocalEndPoint进行调用后,属性通常设置Bind方法。The LocalEndPoint property is usually set after you make a call to the Bind method. 如果你允许系统将套接字的本地 IP 地址和端口号分配LocalEndPoint属性将设置后第一次 I/O 操作。If you allow the system to assign your socket's local IP address and port number, the LocalEndPoint property will be set after the first I/O operation. 对于面向连接的协议,第一次 I/O 操作将是调用ConnectAccept方法。For connection-oriented protocols, the first I/O operation would be a call to the Connect or Accept method. 对于无连接协议,第一次 I/O 操作将是任何发送或接收调用。For connectionless protocols, the first I/O operation would be any of the send or receive calls.

备注

如果你收到SocketException,使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码有关错误的详细说明的文档。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅在.NET Framework 中进行网络跟踪For more information, see Network Tracing in the .NET Framework.

适用于

另请参阅