Socket.RemoteEndPoint Socket.RemoteEndPoint Socket.RemoteEndPoint Socket.RemoteEndPoint Property

定义

获取远程终结点。Gets the remote endpoint.

public:
 property System::Net::EndPoint ^ RemoteEndPoint { System::Net::EndPoint ^ get(); };
public System.Net.EndPoint RemoteEndPoint { get; }
member this.RemoteEndPoint : System.Net.EndPoint
Public ReadOnly Property RemoteEndPoint As EndPoint

属性值

当前和 EndPoint 通信的 SocketThe EndPoint with which the Socket is communicating.

异常

尝试访问套接字时出错。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())

注解

如果使用面向连接的协议RemoteEndPoint属性获取EndPoint,其中包含到的远程 IP 地址和端口号Socket连接。If you are using a connection-oriented protocol, the RemoteEndPoint property gets the EndPoint that contains the remote IP address and port number to which the Socket is connected. 如果使用的是无连接协议RemoteEndPoint包含的默认远程 IP 地址和端口号与Socket将通信。If you are using a connectionless protocol, RemoteEndPoint contains the default remote IP address and port number with which the Socket will communicate. 您必须将此对象转换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 remote IPAddress, and the IPEndPoint.Port method to retrieve the remote port number.

RemoteEndPoint会在调用后设置AcceptConnectThe RemoteEndPoint is set after a call to either Accept or Connect. 如果你尝试访问更早版本,此属性RemoteEndPoint将引发SocketExceptionIf you try to access this property earlier, RemoteEndPoint will throw a SocketException. 如果你收到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.

适用于

另请参阅