This documentation is archived and is not being maintained.
IPEndPoint Class
Visual Studio 2010
Represents a network endpoint as an IP address and a port number.
Assembly: System (in System.dll)
The IPEndPoint type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | IPEndPoint(Int64, Int32) | Initializes a new instance of the IPEndPoint class with the specified address and port number. |
![]() | IPEndPoint(IPAddress, Int32) | Initializes a new instance of the IPEndPoint class with the specified address and port number. |
| Name | Description | |
|---|---|---|
![]() | Address | Gets or sets the IP address of the endpoint. |
![]() | AddressFamily | Gets the Internet Protocol (IP) address family. (Overrides EndPoint::AddressFamily.) |
![]() | Port | Gets or sets the port number of the endpoint. |
| Name | Description | |
|---|---|---|
![]() | Create | Creates an endpoint from a socket address. (Overrides EndPoint::Create(SocketAddress).) |
![]() | Equals | Determines whether the specified Object is equal to the current IPEndPoint instance. (Overrides Object::Equals(Object).) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Returns a hash value for a IPEndPoint instance. (Overrides Object::GetHashCode().) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | Serialize | Serializes endpoint information into a SocketAddress instance. (Overrides EndPoint::Serialize().) |
![]() | ToString | Returns the IP address and port number of the specified endpoint. (Overrides Object::ToString().) |
// 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." ); } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show:
