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获取, 其中包含连接到Socket的远程 IP 地址和端口号。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则包含Socket将与之通信的默认远程 IP 地址和端口号。If you are using a connectionless protocol, RemoteEndPoint contains the default remote IP address and port number with which the Socket will communicate. 在检索任何信息EndPoint IPEndPoint之前, 必须将此转换为。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调用AcceptConnect之后设置。The RemoteEndPoint is set after a call to either Accept or Connect. 如果你之前尝试访问此属性, RemoteEndPointSocketException引发。If 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.

适用于

另请参阅