/samples/snippets/cpp/VS_Snippets_Remoting/IPEndPoint_Properties/CPP/ipendpoint_properties.cpp

https://github.com/dotnet/docs · C++ · 123 lines · 80 code · 13 blank · 30 comment · 5 complexity · 698cbfaf6da332174d168b85e6c39a34 MD5 · raw file

  1. // System.Net.IPEndPoint.MaxPort; System.Net.IPEndPoint.MinPort;
  2. // System.Net.IPEndPoint.AddressFamily; System.Net.IPEndPoint.IPEndPoint(long,int)
  3. // System.Net.IPEndPoint.Address; System.Net.IPEndPoint.Port;
  4. /*This program demonstrates the properties 'MaxPort', 'MinPort','Address','Port'
  5. and 'AddressFamily' and a constructor 'IPEndPoint(long,int)' of class 'IPEndPoint'.
  6. A procedure DoSocketGet is created which internally uses a socket to transmit http "Get" requests to a Web resource.
  7. The program accepts a resource Url, Resolves it to obtain 'IPAddress',Constructs 'IPEndPoint' instance using this
  8. 'IPAddress' and port 80.Invokes DoSocketGet procedure to obtain a response and displays the response to a console.
  9. It then accepts another Url, Resolves it to obtain 'IPAddress'. Assigns this IPAddress and port to the 'IPEndPoint'
  10. and again invokes DoSocketGet to obtain a response and display.
  11. */
  12. #using <System.dll>
  13. using namespace System;
  14. using namespace System::Net;
  15. using namespace System::Text;
  16. using namespace System::Net::Sockets;
  17. using namespace System::Runtime::InteropServices;
  18. String^ DoSocketGet( IPEndPoint^ hostIPEndPoint, String^ getString ); // forward reference
  19. int main()
  20. {
  21. try
  22. {
  23. Console::Write( "\nPlease enter an INTRANET Url as shown: [e.g. www.microsoft.com]:" );
  24. String^ hostString1 = Console::ReadLine();
  25. // <Snippet1>
  26. // <Snippet2>
  27. // <Snippet3>
  28. // <Snippet4>
  29. IPAddress^ hostIPAddress1 = (Dns::Resolve( hostString1 ))->AddressList[ 0 ];
  30. Console::WriteLine( hostIPAddress1 );
  31. IPEndPoint^ hostIPEndPoint = gcnew IPEndPoint( hostIPAddress1,80 );
  32. Console::WriteLine( "\nIPEndPoint information:{0}", hostIPEndPoint );
  33. Console::WriteLine( "\n\tMaximum allowed Port Address :{0}", IPEndPoint::MaxPort );
  34. Console::WriteLine( "\n\tMinimum allowed Port Address :{0}", (int^)IPEndPoint::MinPort );
  35. Console::WriteLine( "\n\tAddress Family :{0}", hostIPEndPoint->AddressFamily );
  36. // </Snippet4>
  37. Console::Write( "\nPress Enter to continue" );
  38. Console::ReadLine();
  39. String^ getString = String::Format( "GET / HTTP/1.1\r\nHost: {0}\r\nConnection: Close\r\n\r\n", hostString1 );
  40. String^ pageContent = DoSocketGet( hostIPEndPoint, getString );
  41. if ( pageContent != nullptr )
  42. {
  43. Console::WriteLine( "Default HTML page on {0} is:\r\n{1}", hostString1, pageContent );
  44. }
  45. // </Snippet3>
  46. // </Snippet2>
  47. // </Snippet1>
  48. Console::Write( "\n\n\nPlease enter another INTRANET Url as shown[e.g. www.microsoft.com]: " );
  49. String^ hostString2 = Console::ReadLine();
  50. // <Snippet5>
  51. // <Snippet6>
  52. IPAddress^ hostIPAddress2 = (Dns::Resolve( hostString2 ))->AddressList[ 0 ];
  53. hostIPEndPoint->Address = hostIPAddress2;
  54. hostIPEndPoint->Port = 80;
  55. getString = String::Format( "GET / HTTP/1.1\r\nHost: {0}\r\nConnection: Close\r\n\r\n", hostString2 );
  56. pageContent = DoSocketGet( hostIPEndPoint, getString );
  57. if ( pageContent != nullptr )
  58. {
  59. Console::WriteLine( "Default HTML page on {0} is:\r\n{1}", hostString2, pageContent );
  60. }
  61. // </Snippet6>
  62. // </Snippet5>
  63. }
  64. catch ( SocketException^ e )
  65. {
  66. Console::WriteLine( "SocketException caught!!!" );
  67. Console::WriteLine( "Source : {0}", e->Source );
  68. Console::WriteLine( "Message : {0}", e->Message );
  69. }
  70. catch ( Exception^ e )
  71. {
  72. Console::WriteLine( "Exception caught!!!" );
  73. Console::WriteLine( "Message : {0}", e->Message );
  74. }
  75. }
  76. String^ DoSocketGet( IPEndPoint^ hostIPEndPoint, String^ getString )
  77. {
  78. try
  79. {
  80. // Set up variables and String to write to the server.
  81. Encoding^ ASCII = Encoding::ASCII;
  82. array<Byte>^ byteGet = ASCII->GetBytes( getString );
  83. array<Byte>^ recvBytes = gcnew array<Byte>(256);
  84. String^ strRetPage = nullptr;
  85. // Create the Socket for sending data over TCP.
  86. Socket^ mySocket = gcnew Socket( AddressFamily::InterNetwork,
  87. SocketType::Stream,ProtocolType::Tcp );
  88. // Connect to host using IPEndPoint.
  89. mySocket->Connect( hostIPEndPoint );
  90. // Send the GET text to the host.
  91. mySocket->Send( byteGet, byteGet->Length, (SocketFlags)( 0 ) );
  92. // Receive the page, loop until all bytes are received.
  93. Int32 byteCount = mySocket->Receive( recvBytes, recvBytes->Length, (SocketFlags)( 0 ) );
  94. strRetPage = String::Concat( strRetPage, ASCII->GetString( recvBytes, 0, byteCount ) );
  95. while ( byteCount > 0 )
  96. {
  97. byteCount = mySocket->Receive( recvBytes, recvBytes->Length, (SocketFlags)( 0 ) );
  98. strRetPage = String::Concat( strRetPage, ASCII->GetString( recvBytes, 0, byteCount ) );
  99. }
  100. return strRetPage;
  101. }
  102. catch ( Exception^ e )
  103. {
  104. Console::WriteLine( "Exception : {0}", e->Message );
  105. Console::WriteLine( "WinSock Error : {0}", Convert::ToString( Marshal::GetLastWin32Error() ) );
  106. return nullptr;
  107. }
  108. }