IPEndPoint クラス
アセンブリ: System (system.dll 内)
構文
解説
IPEndPoint クラスは、ホスト上のサービスに接続するためにアプリケーションが要求するホストとローカル ポートまたはリモート ポートの情報を格納します。ホストの IP アドレスとサービスのポート番号を結合することにより、IPEndPoint クラスはサービスへのコネクション ポイントを形成します。
' This example uses the IPEndPoint class and its members to display the home page ' of the server selected by the user. Imports System Imports System.Text Imports System.IO Imports System.Net Imports System.Net.Sockets Imports System.Text.RegularExpressions Namespace Mssc.Services.ConnectionManagement Module M_TestIPEndPoint Public Class TestIPEndPoint 'The getPage method gets the server's home page content by 'recreating the server's endpoint from the original serialized endpoint. 'Then it creates a new socket and connects it to the endpoint. Private Shared Function getPage(ByVal server As String, ByVal socketAddress As SocketAddress) As String 'Set up variables and String to write to the server. Dim ASCII As Encoding = Encoding.ASCII Dim [Get] As String = "GET / HTTP/1.1" + ControlChars.Cr + ControlChars.Lf + "Host: " + server + ControlChars.Cr + ControlChars.Lf + "Connection: Close" + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf Dim ByteGet As [Byte]() = ASCII.GetBytes([Get]) Dim RecvBytes(255) As [Byte] Dim strRetPage As [String] = Nothing Dim socket As Socket = Nothing ' Recreate the connection endpoint from the serialized information. Dim endpoint As New IPEndPoint(0, 0) Dim clonedIPEndPoint As IPEndPoint = CType(endpoint.Create(socketAddress), IPEndPoint) Console.WriteLine(("clonedIPEndPoint: " + clonedIPEndPoint.ToString())) Console.WriteLine("Press any key to continue.") Console.ReadLine() Try ' Create a socket object to establish a connection with the server. socket = New Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp) ' Connect to the cloned end point. socket.Connect(clonedIPEndPoint) Catch e As SocketException Console.WriteLine(("Source : " + e.Source)) Console.WriteLine(("Message : " + e.Message)) Catch e As Exception Console.WriteLine(("Source : " + e.Source)) Console.WriteLine(("Message : " + e.Message)) End Try If socket Is Nothing Then Return "Connection to cloned endpoint failed" End If ' Send request to the server. socket.Send(ByteGet, ByteGet.Length, 0) ' Receive the server home page content. Dim bytes As Int32 = socket.Receive(RecvBytes, RecvBytes.Length, 0) ' Read the first 256 bytes. strRetPage = "Default HTML page on " + server + ":" + ControlChars.Cr + ControlChars.Lf strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes) While bytes > 0 bytes = socket.Receive(RecvBytes, RecvBytes.Length, 0) strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes) End While socket.Close() Return strRetPage End Function 'getPage ' The serializeEndpoint method serializes the endpoint and returns the ' SocketAddress containing the serialized endpoint data. Private Shared Function serializeEndpoint(ByVal endpoint As IPEndPoint) As SocketAddress ' Serialize IPEndPoint details to a SocketAddress instance. Dim socketAddress As SocketAddress = endpoint.Serialize() ' Display the serialized endpoint information. Console.WriteLine("Endpoint Serialize() : " + socketAddress.ToString()) Console.WriteLine("Socket Family : " + socketAddress.Family.ToString()) Console.WriteLine("Socket Size : " + socketAddress.ToString()) Console.WriteLine("Press any key to continue.") Console.ReadLine() Return socketAddress End Function 'serializeEndpoint Private Shared Sub displayEndpointInfo(ByVal endpoint As IPEndPoint) Console.WriteLine("Endpoint Address : " + endpoint.Address.ToString()) Console.WriteLine("Endpoint AddressFamily : " + endpoint.AddressFamily.ToString()) Console.WriteLine("Endpoint Port : " + endpoint.Port.ToString()) Console.WriteLine("Endpoint ToString() : " + endpoint.ToString()) Console.WriteLine("Press any key to continue.") Console.ReadLine() End Sub 'displayEndpointInfo ' The following method determines the server endpoint and then ' serializes it to obtain the related SocketAddress object. ' Note that in the for loop a temporary socket is created to ensure that ' the current IP address format matches the AddressFamily type. ' In fact, in the case of servers supporting both IPv4 and IPv6, an exception ' may arise if an IP address format does not match the address family type. Private Shared Function getSocketAddress(ByVal server As String, ByVal port As Integer) As SocketAddress Dim tempSocket As Socket = Nothing Dim host As IPHostEntry = Nothing Dim serializedSocketAddress As SocketAddress = Nothing Try ' Get the object containing Internet host information. host = Dns.Resolve(server) ' 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 'Close the temporary socket. tempSocket.Close() Catch e As SocketException Console.WriteLine(("Source : " + e.Source)) Console.WriteLine(("Message : " + e.Message)) Catch e As Exception Console.WriteLine(("Source : " + e.Source)) Console.WriteLine(("Message : " + e.Message)) End Try Return serializedSocketAddress End Function 'getSocketAddress ' The requestServerHomePage obtains the server's home page and returns ' its content. Private Shared Function requestServerHomePage(ByVal server As String, ByVal port As Integer) As String Dim strRetPage As [String] = Nothing ' Get a socket address using the specified server and port. Dim socketAddress As SocketAddress = getSocketAddress(server, port) If socketAddress Is Nothing Then strRetPage = "Connection failed" ' Obtain the server's home page content. Else strRetPage = getPage(server, socketAddress) End If Return strRetPage End Function 'requestServerHomePage ' Show to the user how to use this program when wrong input parameters are entered. Private Shared Sub showusage() Console.WriteLine("Enter the server name as follows:") Console.WriteLine(ControlChars.Tab + "vb_ipendpoint servername") End Sub 'showusage ' This is the program entry point. It allows the user to enter ' a server name that is used to locate its current homepage. Public Shared Sub Main(ByVal args() As String) Dim host As String = Nothing Dim port As Integer = 80 'Define a regular expression to parse user's input. 'This is a security check. It allows only 'alphanumeric input string between 2 to 40 character long. Dim rex As New Regex("^[a-zA-Z]\w{1 ,39}$") If args.Length = 0 Then ' Show how to use this program. showusage() Else host = args(0) If ((rex.Match(host)).Success) Then ' Get the specified server home_page and display its content. Dim result As String = requestServerHomePage(host, port) Console.WriteLine(result) Else Console.WriteLine("Input string format not allowed.") End If End If End Sub 'Main End Class 'TestIPEndPoint End Module End Namespace
// This example uses the IPEndPoint class and its members to display the home page // of the server selected by the user. using System; using System.Text; using System.IO; using System.Net; using System.Net.Sockets; using System.Text.RegularExpressions; namespace Mssc.Services.ConnectionManagement { public class TestIPEndPoint { // The getPage method gets the server's home page content by // recreating the server's endpoint from the original serialized endpoint. // Then it creates a new socket and connects it to the endpoint. private static string getPage(string server, SocketAddress socketAddress) { //Set up variables and string to write to the server. Encoding ASCII = Encoding.ASCII; string Get = "GET / HTTP/1.1\r\nHost: " + server + "\r\nConnection: Close\r\n\r\n"; Byte[] ByteGet = ASCII.GetBytes(Get); Byte[] RecvBytes = new Byte[256]; String strRetPage = null; Socket socket = null; // Recreate the connection endpoint from the serialized information. IPEndPoint endpoint = new IPEndPoint(0,0); IPEndPoint clonedIPEndPoint = (IPEndPoint) endpoint.Create(socketAddress); Console.WriteLine("clonedIPEndPoint: " + clonedIPEndPoint.ToString()); Console.WriteLine("Press any key to continue."); Console.ReadLine(); try { // Create a socket object to establish a connection with the server. socket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Connect to the cloned end point. socket.Connect(clonedIPEndPoint); } catch(SocketException e) { Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } catch(Exception e) { Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } if (socket == null) return ("Connection to cloned endpoint failed"); // Send request to the server. socket.Send(ByteGet, ByteGet.Length, 0); // Receive the server home page content. Int32 bytes = socket.Receive(RecvBytes, RecvBytes.Length, 0); // Read the first 256 bytes. strRetPage = "Default HTML page on " + server + ":\r\n"; strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes); while (bytes > 0) { bytes = socket.Receive(RecvBytes, RecvBytes.Length, 0); strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes); } socket.Close(); return strRetPage; } // The serializeEndpoint method serializes the endpoint and returns the // SocketAddress containing the serialized endpoint data. private static SocketAddress serializeEndpoint(IPEndPoint endpoint) { // Serialize IPEndPoint details to a SocketAddress instance. SocketAddress socketAddress = endpoint.Serialize(); // Display the serialized endpoint information. Console.WriteLine("Endpoint.Serialize() : " + socketAddress.ToString()); Console.WriteLine("Socket.Family : " + socketAddress.Family); Console.WriteLine("Socket.Size : " + socketAddress.Size); Console.WriteLine("Press any key to continue."); Console.ReadLine(); return socketAddress; } private static void displayEndpointInfo(IPEndPoint endpoint) { Console.WriteLine("Endpoint.Address : " + endpoint.Address); Console.WriteLine("Endpoint.AddressFamily : " + endpoint.AddressFamily); Console.WriteLine("Endpoint.Port : " + endpoint.Port); Console.WriteLine("Endpoint.ToString() : " + endpoint.ToString()); Console.WriteLine("Press any key to continue."); Console.ReadLine(); } // The serializeEndpoint method determines the server endpoint and then // serializes it to obtain the related SocketAddress object. // Note that in the for loop a temporary socket is created to ensure that // the current IP address format matches the AddressFamily type. // In fact, in the case of servers supporting both IPv4 and IPv6, an exception // may arise if an IP address format does not match the address family type. private static SocketAddress getSocketAddress(string server, int port) { Socket tempSocket = null; IPHostEntry host = null; SocketAddress serializedSocketAddress = null; try { // Get the object containing Internet host information. host = Dns.Resolve(server); // 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; } // Close the temporary socket. tempSocket.Close(); } catch(SocketException e) { Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } catch(Exception e) { Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } return serializedSocketAddress; } // The requestServerHomePage method obtains the server's home page and returns // its content. private static string requestServerHomePage(string server, int port) { String strRetPage = null; // Get a socket address using the specified server and port. SocketAddress socketAddress = getSocketAddress(server, port); if (socketAddress == null) strRetPage = "Connection failed"; else // Obtain the server's home page content. strRetPage = getPage(server, socketAddress); return strRetPage; } // Show to the user how to use this program when wrong input parameters are entered. private static void showUsage() { Console.WriteLine("Enter the server name as follows:"); Console.WriteLine("\tcs_ipendpoint servername"); } // This is the program entry point. It allows the user to enter // a server name that is used to locate its current homepage. public static void Main(string[] args) { string host= null; int port = 80; // Define a regular expression to parse user's input. // This is a security check. It allows only // alphanumeric input string between 2 to 40 character long. Regex rex = new Regex(@"^[a-zA-Z]\w{1,39}$"); if (args.Length < 1) showUsage(); else { string message = args[0]; if ((rex.Match(message)).Success) { host = args[0]; // Get the specified server home_page and display its content. string result = requestServerHomePage(host, port); Console.WriteLine(result); } else Console.WriteLine("Input string format not allowed."); } } } }
// This example uses the IPEndPoint class and its members to display the home page // of the server selected by the user. #using <System.dll> using namespace System; using namespace System::Text; using namespace System::IO; using namespace System::Net; using namespace System::Net::Sockets; using namespace System::Text::RegularExpressions; // The getPage function gets the server's home page content by // recreating the server's endpoint from the original serialized endpoint. // Then it creates a new socket and connects it to the endpoint. String^ getPage( String^ server, SocketAddress^ socketAddress ) { //Set up variables and string to write to the server. Encoding^ ASCII = Encoding::ASCII; String^ Get = String::Format( "GET / HTTP/1.1\r\nHost: {0}\r\nConnection: Close\r\n\r\n", server ); array<Byte>^ByteGet = ASCII->GetBytes( Get ); array<Byte>^RecvBytes = gcnew array<Byte>(256); String^ strRetPage = nullptr; Socket^ socket = nullptr; // Recreate the connection endpoint from the serialized information. IPEndPoint^ endpoint = gcnew IPEndPoint( (__int64)0,0 ); IPEndPoint^ clonedIPEndPoint = dynamic_cast<IPEndPoint^>(endpoint->Create( socketAddress )); Console::WriteLine( "clonedIPEndPoint: {0}", clonedIPEndPoint ); Console::WriteLine( "Press any key to continue." ); Console::ReadLine(); try { // Create a socket object to establish a connection with the server. socket = gcnew Socket( endpoint->AddressFamily,SocketType::Stream,ProtocolType::Tcp ); // Connect to the cloned end point. socket->Connect( clonedIPEndPoint ); } catch ( SocketException^ e ) { Console::WriteLine( "Source : {0}", e->Source ); Console::WriteLine( "Message : {0}", e->Message ); } catch ( Exception^ e ) { Console::WriteLine( "Source : {0}", e->Source ); Console::WriteLine( "Message : {0}", e->Message ); } if ( socket == nullptr ) return ("Connection to cloned endpoint failed"); // Send request to the server. socket->Send( ByteGet, ByteGet->Length, static_cast<SocketFlags>(0) ); // Receive the server home page content. Int32 bytes = socket->Receive( RecvBytes, RecvBytes->Length, static_cast<SocketFlags>(0) ); // Read the first 256 bytes. strRetPage = String::Format( "Default HTML page on {0}:\r\n", server ); strRetPage = String::Concat( strRetPage, ASCII->GetString( RecvBytes, 0, bytes ) ); while ( bytes > 0 ) { bytes = socket->Receive( RecvBytes, RecvBytes->Length, static_cast<SocketFlags>(0) ); strRetPage = String::Concat( strRetPage, ASCII->GetString( RecvBytes, 0, bytes ) ); } socket->Close(); return strRetPage; } // The serializeEndpoint function serializes the endpoint and returns the // SocketAddress containing the serialized endpoint data. SocketAddress^ serializeEndpoint( IPEndPoint^ endpoint ) { // Serialize IPEndPoint details to a SocketAddress instance. SocketAddress^ socketAddress = endpoint->Serialize(); // Display the serialized endpoint information. Console::WriteLine( "Endpoint.Serialize() : {0}", socketAddress ); Console::WriteLine( "Socket->Family : {0}", socketAddress->Family ); Console::WriteLine( "Socket->Size : {0}", socketAddress->Size ); Console::WriteLine( "Press any key to continue." ); Console::ReadLine(); return socketAddress; } void displayEndpointInfo( IPEndPoint^ endpoint ) { Console::WriteLine( "Endpoint->Address : {0}", endpoint->Address ); Console::WriteLine( "Endpoint->AddressFamily : {0}", endpoint->AddressFamily ); Console::WriteLine( "Endpoint->Port : {0}", endpoint->Port ); Console::WriteLine( "Endpoint.ToString() : {0}", endpoint ); Console::WriteLine( "Press any key to continue." ); Console::ReadLine(); } // The serializeEndpoint function determines the server endpoint and then // serializes it to obtain the related SocketAddress object. // Note that in the for loop a temporary socket is created to ensure that // the current IP address format matches the AddressFamily type. // In fact, in the case of servers supporting both IPv4 and IPv6, an exception // may arise if an IP address format does not match the address family type. SocketAddress^ getSocketAddress( String^ server, int port ) { Socket^ tempSocket = nullptr; IPHostEntry^ host = nullptr; SocketAddress^ serializedSocketAddress = nullptr; try { // Get the object containing Internet host information. host = Dns::Resolve( server ); // 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; } // Close the temporary socket. tempSocket->Close(); } catch ( SocketException^ e ) { Console::WriteLine( "Source : {0}", e->Source ); Console::WriteLine( "Message : {0}", e->Message ); } catch ( Exception^ e ) { Console::WriteLine( "Source : {0}", e->Source ); Console::WriteLine( "Message : {0}", e->Message ); } return serializedSocketAddress; } // The requestServerHomePage function obtains the server's home page and returns // its content. String^ requestServerHomePage( String^ server, int port ) { String^ strRetPage = nullptr; // Get a socket address using the specified server and port. SocketAddress^ socketAddress = getSocketAddress( server, port ); if ( socketAddress == nullptr ) strRetPage = "Connection failed"; // Obtain the server's home page content. else strRetPage = getPage( server, socketAddress ); return strRetPage; } // Show to the user how to use this program when wrong input parameters are entered. void showUsage() { Console::WriteLine( "Enter the server name as follows:" ); Console::WriteLine( "\tcs_ipendpoint servername" ); } // This is the program entry point. It allows the user to enter // a server name that is used to locate its current homepage. int main() { array<String^>^args = Environment::GetCommandLineArgs(); String^ host = nullptr; int port = 80; // Define a regular expression to parse user's input. // This is a security check. It allows only // alphanumeric input string between 2 to 40 character long. Regex^ rex = gcnew Regex( "^[a-zA-Z]\\w{1,39}$" ); if ( args->Length < 2 ) showUsage(); else { String^ message = args[ 1 ]; if ( (rex->Match(message))->Success ) { host = args[ 1 ]; // Get the specified server home_page and display its content. String^ result = requestServerHomePage( host, port ); Console::WriteLine( result ); } else Console::WriteLine( "Input string format not allowed." ); } }
// This example uses the IPEndPoint class and its members to display the // home page of the server selected by the user. import System.*; import System.Text.*; import System.IO.*; import System.Net.*; import System.Net.Sockets.*; import System.Text.RegularExpressions.*; public class TestIPEndPoint { // The GetPage method gets the server's home page content by // recreating the server's endpoint from the original serialized endpoint. // Then it creates a new socket and connects it to the endpoint. private static String GetPage(String server, SocketAddress socketAddress) { //Set up variables and string to write to the server. Encoding ascii = Encoding.get_ASCII(); String get = "GET / HTTP/1.1\r\nHost: " + server + "\r\nConnection: Close\r\n\r\n"; ubyte byteGet[] = ascii.GetBytes(get); ubyte recvBytes[] = new ubyte[256]; String strRetPage = null; Socket socket = null; // Recreate the connection endpoint from the serialized information. IPEndPoint endpoint = new IPEndPoint(0, 0); IPEndPoint clonedIPEndPoint = ((IPEndPoint)(endpoint.Create(socketAddress))); Console.WriteLine(("clonedIPEndPoint: " + clonedIPEndPoint.ToString())); Console.WriteLine("Press any key to continue."); Console.ReadLine(); try { // Create a socket object to establish a connection // with the server. socket = new Socket(endpoint.get_AddressFamily(), SocketType.Stream, ProtocolType.Tcp); // Connect to the cloned end point. socket.Connect(clonedIPEndPoint); } catch (SocketException e) { Console.WriteLine(("Source : " + e.get_Source())); Console.WriteLine(("Message : " + e.get_Message())); } catch (System.Exception e) { Console.WriteLine(("Source : " + e.get_Source())); Console.WriteLine(("Message : " + e.get_Message())); } if (socket == null) { return "Connection to cloned endpoint failed"; } // Send request to the server. socket.Send(byteGet, byteGet.length, (SocketFlags)0); // Receive the server home page content. int bytes = socket.Receive(recvBytes, recvBytes.length, (SocketFlags)0); // Read the first 256 bytes. strRetPage = "Default HTML page on " + server + ":\r\n"; strRetPage = strRetPage + ascii.GetString(recvBytes, 0, bytes); while ((bytes > 0)) { bytes = socket.Receive(recvBytes, recvBytes.length, (SocketFlags)0); strRetPage = strRetPage + ascii.GetString(recvBytes, 0, bytes); } socket.Close(); return strRetPage; } //GetPage // The SerializeEndpoint method serializes the endpoint and returns the // SocketAddress containing the serialized endpoint data. private static SocketAddress SerializeEndpoint(IPEndPoint endpoint) { // Serialize IPEndPoint details to a SocketAddress instance. SocketAddress socketAddress = endpoint.Serialize(); // Display the serialized endpoint information. Console.WriteLine(("Endpoint.Serialize() : " + socketAddress.ToString())); Console.WriteLine(("Socket.Family : " + socketAddress.get_Family())); Console.WriteLine(("Socket.Size : " + socketAddress.get_Size())); Console.WriteLine("Press any key to continue."); Console.ReadLine(); return socketAddress; } //SerializeEndpoint private static void DisplayEndpointInfo(IPEndPoint endpoint) { Console.WriteLine(("Endpoint.Address : " + endpoint.get_Address())); Console.WriteLine(("Endpoint.AddressFamily : " + endpoint.get_AddressFamily())); Console.WriteLine(("Endpoint.Port : " + endpoint.get_Port())); Console.WriteLine(("Endpoint.ToString() : " + endpoint.ToString())); Console.WriteLine("Press any key to continue."); Console.ReadLine(); } //DisplayEndpointInfo // The SerializeEndpoint method determines the server endpoint and then // serializes it to obtain the related SocketAddress object. // Note that in the for loop a temporary socket is created to ensure that // the current IP address format matches the AddressFamily type. // In fact, in the case of servers supporting both IPv4 and IPv6, // an exception may arise if an IP address format does not match the // address family type. private static SocketAddress GetSocketAddress(String server, int port) { Socket tempSocket = null; IPHostEntry host = null; SocketAddress serializedSocketAddress = null; try { // get the object containing Internet host information. host = Dns.Resolve(server); // Obtain the IP address from the list of IP addresses // associated with the server. for (int iCtr = 0; iCtr < host.get_AddressList().length; iCtr++) { IPAddress address = host.get_AddressList()[iCtr]; IPEndPoint endpoint = new IPEndPoint(address, port); tempSocket = new Socket(endpoint.get_AddressFamily(), SocketType.Stream, ProtocolType.Tcp); tempSocket.Connect(endpoint); if (tempSocket.get_Connected()) { // Display the endpoint information. DisplayEndpointInfo(endpoint); // Serialize the endpoint to obtain a SocketAddress object. serializedSocketAddress = SerializeEndpoint(endpoint); break; } else { continue; } } // Close the temporary socket. tempSocket.Close(); } catch (SocketException e) { Console.WriteLine(("Source : " + e.get_Source())); Console.WriteLine(("Message : " + e.get_Message())); } catch (System.Exception e) { Console.WriteLine(("Source : " + e.get_Source())); Console.WriteLine(("Message : " + e.get_Message())); } return serializedSocketAddress; } //GetSocketAddress // The RequestServerHomePage method obtains the server's home page and // returns its content. private static String RequestServerHomePage(String server, int port) { String strRetPage = null; // Get a socket address using the specified server and port. SocketAddress socketAddress = GetSocketAddress(server, port); if (socketAddress == null) { strRetPage = "Connection failed"; } else { // Obtain the server's home page content. strRetPage = GetPage(server, socketAddress); } return strRetPage; } //RequestServerHomePage // Show to the user how to use this program when wrong input parameters // are entered. private static void ShowUsage() { Console.WriteLine("Enter the server name as follows:"); Console.WriteLine("\tcs_ipendpoint servername"); } //ShowUsage // This is the program entry point. It allows the user to enter // a server name that is used to locate its current homepage. public static void main(String[] args) { String host = null; int port = 80; // Define a regular expression to parse user's input. // This is a security check. It allows only // alphanumeric input string between 2 to 40 character long. Regex rex = new Regex("^[a-zA-Z]\\w{1,39}$"); if (args.length < 1) { ShowUsage(); } else { String message = args[0]; if (rex.Match(message).get_Success()) { host = args[0]; // Get the specified server home_page and display its content. String result = RequestServerHomePage(host, port); Console.WriteLine(result); } else { Console.WriteLine("Input string format not allowed."); } } } //main } //TestIPEndPoint
System.Net.EndPoint
System.Net.IPEndPoint
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
IPEndPoint コンストラクタ (Int64, Int32)
アセンブリ: System (system.dll 内)
構文
例外
指定した IP アドレスとポート番号を使用して IPEndPoint を作成する例を次に示します。
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()))
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);
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 );
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
IPEndPoint コンストラクタ (IPAddress, Int32)
アセンブリ: System (system.dll 内)
構文
例外
' 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
// 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. 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. for (int iCtr = 0; iCtr < host.get_AddressList().length; iCtr++) { IPAddress address = host.get_AddressList()[iCtr]; IPEndPoint endpoint = new IPEndPoint(address, port); tempSocket = new Socket(endpoint.get_AddressFamily(), SocketType.Stream, ProtocolType.Tcp); tempSocket.Connect(endpoint); if (tempSocket.get_Connected()) { // Display the endpoint information. DisplayEndpointInfo(endpoint); // Serialize the endpoint to obtain a SocketAddress object. serializedSocketAddress = SerializeEndpoint(endpoint); break; } else { continue; } }
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
IPEndPoint コンストラクタ
オーバーロードの一覧
名前 | 説明 |
---|---|
IPEndPoint (Int64, Int32) | 指定したアドレスとポート番号を使用して、IPEndPoint クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
IPEndPoint (IPAddress, Int32) | 指定したアドレスとポート番号を使用して、IPEndPoint クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
IPEndPoint フィールド
IPEndPoint プロパティ
パブリック プロパティ
名前 | 説明 | |
---|---|---|
AddressFamily | オーバーライドされます。 インターネット プロトコル (IP: Internet Protocol) アドレス ファミリを取得します。 | |
Port | エンドポイントのポート番号を取得または設定します。 |
IPEndPoint メソッド
パブリック メソッド
名前 | 説明 | |
---|---|---|
Create | オーバーライドされます。 ソケット アドレスからエンドポイントを作成します。 | |
Equals | オーバーロードされます。 オーバーライドされます。 | |
GetHashCode | オーバーライドされます。 | |
GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) | |
ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) | |
Serialize | オーバーライドされます。 SocketAddress インスタンスに対してエンドポイント情報をシリアル化します。 | |
ToString | オーバーライドされます。 指定したエンドポイントの IP アドレスとポート番号を返します。 |
名前 | 説明 | |
---|---|---|
Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) | |
MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
IPEndPoint メンバ
IP アドレスとポート番号でネットワーク エンドポイントを表します。
IPEndPoint データ型で公開されるメンバを以下の表に示します。
パブリック コンストラクタ
パブリック フィールド
パブリック プロパティ
名前 | 説明 | |
---|---|---|
AddressFamily | オーバーライドされます。 インターネット プロトコル (IP: Internet Protocol) アドレス ファミリを取得します。 | |
Port | エンドポイントのポート番号を取得または設定します。 |
名前 | 説明 | |
---|---|---|
Create | オーバーライドされます。 ソケット アドレスからエンドポイントを作成します。 | |
Equals | オーバーロードされます。 オーバーライドされます。 | |
GetHashCode | オーバーライドされます。 | |
GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) | |
ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) | |
Serialize | オーバーライドされます。 SocketAddress インスタンスに対してエンドポイント情報をシリアル化します。 | |
ToString | オーバーライドされます。 指定したエンドポイントの IP アドレスとポート番号を返します。 |
名前 | 説明 | |
---|---|---|
Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) | |
MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
- IPEndPointのページへのリンク