IPEndPoint 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 IPEndPoint 類別的新執行個體。
多載
IPEndPoint(Int64, Int32) |
使用指定的位址和通訊埠編號來初始化 IPEndPoint 類別的新執行個體。 |
IPEndPoint(IPAddress, Int32) |
使用指定的位址和通訊埠編號來初始化 IPEndPoint 類別的新執行個體。 |
IPEndPoint(Int64, Int32)
使用指定的位址和通訊埠編號來初始化 IPEndPoint 類別的新執行個體。
public:
IPEndPoint(long address, int port);
public IPEndPoint (long address, int port);
new System.Net.IPEndPoint : int64 * int -> System.Net.IPEndPoint
Public Sub New (address As Long, port As Integer)
參數
- address
- Int64
網際網路主機的 IP 位址。 例如,位元組由大到小格式的 0x2414188f 值將為 IP 位址 "143.24.20.36"。
- port
- Int32
與 address
相關聯的通訊埠編號,或 0 指定任何可用的通訊埠。
port
依主機順序顯示。
例外狀況
範例
下列範例會使用指定的 IP 位址與連接埠號碼來建立 IPEndPoint。
IPAddress^ hostIPAddress1 = (Dns::Resolve( hostString1 ))->AddressList[ 0 ];
Console::WriteLine( hostIPAddress1 );
IPEndPoint^ hostIPEndPoint = gcnew IPEndPoint( hostIPAddress1,80 );
Console::WriteLine( "\nIPEndPoint information:{0}", hostIPEndPoint );
Console::WriteLine( "\n\tMaximum allowed Port Address :{0}", IPEndPoint::MaxPort );
Console::WriteLine( "\n\tMinimum allowed Port Address :{0}", (int^)IPEndPoint::MinPort );
Console::WriteLine( "\n\tAddress Family :{0}", hostIPEndPoint->AddressFamily );
IPAddress hostIPAddress1 = (Dns.Resolve(hostString1)).AddressList[0];
Console.WriteLine(hostIPAddress1.ToString());
IPEndPoint hostIPEndPoint = new IPEndPoint(hostIPAddress1,80);
Console.WriteLine("\nIPEndPoint information:" + hostIPEndPoint.ToString());
Console.WriteLine("\n\tMaximum allowed Port Address :" + IPEndPoint.MaxPort);
Console.WriteLine("\n\tMinimum allowed Port Address :" + IPEndPoint.MinPort);
Console.WriteLine("\n\tAddress Family :" + hostIPEndPoint.AddressFamily);
Dim hostIPAddress1 As IPAddress = Dns.Resolve(hostString1).AddressList(0)
Dim hostIPEndPoint As New IPEndPoint(hostIPAddress1, 80)
Console.WriteLine((ControlChars.Cr + "IPEndPoint information:" + hostIPEndPoint.ToString()))
Console.WriteLine((ControlChars.Cr + ControlChars.Tab + "Maximum allowed Port Address :" + IPEndPoint.MaxPort.ToString()))
Console.WriteLine((ControlChars.Cr + ControlChars.Tab + "Minimum allowed Port Address :" + IPEndPoint.MinPort.ToString()))
Console.WriteLine((ControlChars.Cr + ControlChars.Tab + "Address Family :" + hostIPEndPoint.AddressFamily.ToString()))
適用於
IPEndPoint(IPAddress, Int32)
使用指定的位址和通訊埠編號來初始化 IPEndPoint 類別的新執行個體。
public:
IPEndPoint(System::Net::IPAddress ^ address, int port);
public IPEndPoint (System.Net.IPAddress address, int port);
new System.Net.IPEndPoint : System.Net.IPAddress * int -> System.Net.IPEndPoint
Public Sub New (address As IPAddress, port As Integer)
參數
- port
- Int32
與 address
相關聯的通訊埠編號,或 0 指定任何可用的通訊埠。
port
依主機順序顯示。
例外狀況
address
為 null
。
範例
// Obtain the IP address from the list of IP addresses associated with the server.
System::Collections::IEnumerator^ myEnum = host->AddressList->GetEnumerator();
while ( myEnum->MoveNext() )
{
IPAddress^ address = safe_cast<IPAddress^>(myEnum->Current);
IPEndPoint^ endpoint = gcnew IPEndPoint( address,port );
tempSocket = gcnew Socket( endpoint->AddressFamily,SocketType::Stream,ProtocolType::Tcp );
tempSocket->Connect( endpoint );
if ( tempSocket->Connected )
{
// Display the endpoint information.
displayEndpointInfo( endpoint );
// Serialize the endpoint to obtain a SocketAddress object.
serializedSocketAddress = serializeEndpoint( endpoint );
break;
}
else
continue;
}
// Obtain the IP address from the list of IP addresses associated with the server.
foreach(IPAddress address in host.AddressList)
{
IPEndPoint endpoint = new IPEndPoint(address, port);
tempSocket =
new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tempSocket.Connect(endpoint);
if(tempSocket.Connected)
{
// Display the endpoint information.
displayEndpointInfo(endpoint);
// Serialize the endpoint to obtain a SocketAddress object.
serializedSocketAddress = serializeEndpoint(endpoint);
break;
}
else
{
continue;
}
}
' Obtain the IP address from the list of IP addresses associated with the server.
Dim address As IPAddress
For Each address In host.AddressList
Dim endpoint As New IPEndPoint(address, port)
tempSocket = New Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
tempSocket.Connect(endpoint)
If tempSocket.Connected Then
' Display the endpoint information.
displayEndpointInfo(endpoint)
' Serialize the endpoint to obtain a SocketAddress object.
serializedSocketAddress = serializeEndpoint(endpoint)
Exit For
End If
Next address