Socket.RemoteEndPoint 屬性

定義

取得遠端端點。

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

屬性值

EndPointSocket 正在與其通訊。

例外狀況

嘗試存取通訊端時發生錯誤。

範例

下列程式碼範例會擷取並顯示本機和遠端端點。

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 。 如果您使用無連線通訊協定, RemoteEndPoint 則包含將與其通訊的預設遠端 IP 位址和埠號碼 Socket 。 您必須先將此 EndPoint 轉換成 , IPEndPoint 才能擷取任何資訊。 接著,您可以呼叫 IPEndPoint.Address 方法來擷取遠端 IPAddress ,以及 IPEndPoint.Port 擷取遠端埠號碼的方法。

會在 RemoteEndPoint 呼叫 AcceptConnect 之後設定 。 如果您稍早嘗試存取這個屬性, RemoteEndPoint 將會擲回 SocketException 。 如果您收到 SocketException ,請使用 SocketException.ErrorCode 屬性來取得特定的錯誤碼。 取得此程式碼之後,請參閱 Windows Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework 中的網路追蹤

適用於

另請參閱