< Intro To System.Net Namespace | Main | Socket-Level Classes >

 


 

Chapter 6 Part 2:

Introduction to System.Net

 

 

What do we have in this chapter 6 Part 2?

  1. C++ Services and Types Program Example

  2. C# Services and Types Program Example

  3. VB .NET Services and Types Program Example

 

 

C++ Services and Types Program Example

 

Create a new CLR console application and you can use TypesServicesChap6CP as the project and solution names if you want.

 

C++ Services and Types Program Example - creating a new CLR console application project using Visual Studio 2008

 

Add the following code.

 

// TypesServicesChap6CP.cpp : main project file.

/// <summary>

/// This sample demonstrates some of the operations that

/// can be performed using the basic types and services provided in System.Net.

/// </summary>

#include "stdafx.h"

 

using namespace System;

using namespace System::Net;

 

static void ResolveIP(String^ IPAddr)

{

    try

    {

          IPHostEntry^ hostInfo = Dns::GetHostEntry(IPAddr);

          for each (IPAddress^ address in hostInfo->AddressList)

                Console::WriteLine(hostInfo->HostName->ToString());

    }

    catch (Exception^ ex)

    {

          Console::WriteLine(ex->ToString());

    }

    finally

    {

          Console::WriteLine("Done...");

    }

}

 

static void ResolveName(String^ name)

{

    try

    {

        IPHostEntry^ hostInfo = Dns::GetHostEntry(name);

        for each (IPAddress^ address in hostInfo->AddressList)

              Console::WriteLine(address->ToString());

    }

    catch (Exception^ ex)

    {

        Console::WriteLine(ex->ToString());

    }

    finally

    {

        Console::WriteLine("Done...");

     }

}

 

static void RequestProtectedResource(String^ resource)

{

            try

            {

                        Console::WriteLine("Resource: {0}", resource);

                        Console::WriteLine("Please enter your user name.");

                        String^ userName = Console::ReadLine();

                        Console::WriteLine("Please enter your password.");

                        String^ password = Console::ReadLine();

                        WebClient^ client = gcnew WebClient();

                        client->Credentials = gcnew NetworkCredential(userName, password);

                        client->DownloadFile(resource, "page.htm");

                        Console::WriteLine("Got access and downloading a page...");

            }

            catch(Exception^ err)

            {

                        Console::WriteLine("Some error: " + err->Message);

            }

            finally

            {

                        Console::WriteLine("Done...");

            }

}

 

static void ParseIPAddress(String^ addressString)

{

    try

    {

          IPAddress^ address = IPAddress::Parse(addressString);

          Console::WriteLine("The address is " + address->ToString() + " and is of the family "

                                           + address->AddressFamily.ToString() + ".");

    }

    catch (Exception^ ex)

    {

           Console::WriteLine("Failure parsing " + addressString + ". " + ex->Message);

    }

    finally

    {

            Console::WriteLine("Done...");

     }

}

 

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

int main(array<System::String ^> ^args)

{

            Console::WriteLine("Testing...");

            Console::WriteLine("IPv4?");

            ParseIPAddress("64.233.167.99"); // IPv4 Address sample

            Console::WriteLine();

            Console::WriteLine("IPv6?");

            ParseIPAddress("::1"); // IPv6 Address sample

            Console::WriteLine();

            Console::WriteLine("Invalid IP address...");

            ParseIPAddress("::1::2"); // Invalid Address sample

            Console::WriteLine();

 

            Console::WriteLine("Another invalid IP address...");

            ParseIPAddress("255.255.256.0"); // Invalid Address sample

            Console::WriteLine();

 

            Console::WriteLine("HTTP protected resources...");

            // Change the uri accordingly for testing...

            RequestProtectedResource("http://www.contoso.com/protected/resource.htm");

            Console::WriteLine();

            Console::WriteLine("FTP protected resources...");

            // Change the uri accordingly for testing...

            RequestProtectedResource("ftp://ftp.microsoft.com/Products/mspress/library/animat.zip");

            Console::WriteLine();

 

            Console::WriteLine("Resolving www.youtube.com to IP...");

            ResolveName("www.youtube.com");

            Console::WriteLine();

            Console::WriteLine("Resolving www.google.com to IP...");

            ResolveName("www.google.com");

 

            Console::WriteLine();

            Console::WriteLine("Resolving IP to name...");

            ResolveIP("87.248.113.14");

            Console::WriteLine();

            Console::WriteLine("Resolving IP to name...");

            ResolveIP("203.106.85.65");

 

    return 0;

}

 

Build and run the project. The following is the output sample.

 

C++ Services and Types Program Example - a sample output using hardcoded arguments

 

C# Services and Types Program Example

 

Create a new console application project. You can use the project and solution names as shown in the following Figure if you want.

 

  C# Services and Types Program Example - creating a new console application project

 

Add the following code.

 

using System;

using System.Net;

 

namespace TypesServicesChap6CS

{

    /// <summary>

    /// This sample demonstrates some of the operations that

    /// can be performed using the basic types and services provided in System.Net.

    /// </summary>

    class Program

    {

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main(string[] args)

        {

            Console.WriteLine("Testing...");

            Console.WriteLine("IPv4?");

            ParseIPAddress("64.233.167.99"); // IPv4 Address

            Console.WriteLine();

            Console.WriteLine("IPv6?");

            ParseIPAddress("::1"); // IPv6 Address

            Console.WriteLine();

            Console.WriteLine("Invalid IP address...");

            ParseIPAddress("::1::2"); // Invalid Address

            Console.WriteLine();

            Console.WriteLine("Another invalid IP address...");

            ParseIPAddress("255.255.256.0"); // Invalid Address

            Console.WriteLine();

            Console.WriteLine("HTTP protected resources...");

            RequestProtectedResource("http://www.contoso.com/protected/resource.htm");

            Console.WriteLine();

            Console.WriteLine("FTP protected resources...");

            // Change the URI accordingly for testing...

            RequestProtectedResource("ftp://ftp.microsoft.com/Products/mspress/library/animat.zip");

            Console.WriteLine();

            Console.WriteLine("Resolving www.yahoo.com to IP...");

            ResolveName("www.yahoo.com");

        }

 

        static void ResolveName(string name)

        {

            try

            {

                IPHostEntry hostInfo = Dns.GetHostEntry(name);

                foreach (IPAddress address in hostInfo.AddressList)

                    Console.WriteLine(address.ToString());

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.ToString());

            }

        }

 

        static void RequestProtectedResource(string resource)

        {

            Console.WriteLine("Please enter your user name.");

            string userName = Console.ReadLine();

            Console.WriteLine("Please enter your password.");

            string password = Console.ReadLine();

            WebClient client = new WebClient();

            client.Credentials = new NetworkCredential(userName, password);

            client.DownloadFile(resource, "page.htm");

        }

 

        static void ParseIPAddress(string addressString)

        {

            try

            {

                IPAddress address = IPAddress.Parse(addressString);

                Console.WriteLine("The address is " + address.ToString()

                + " and is of the family " + address.AddressFamily.ToString() + ".");

            }

            catch (Exception ex)

            {

                Console.WriteLine("Failure parsing " + addressString + ". " + ex.ToString());

            }

        }

    }

}

 

Build and run the project (Start Without Debugging). The following is a sample output.

 

C# Services and Types Program Example - a sample output using default arguments

 

VB .NET Services and Types Program Example

 

Create a new console application project. You can use the project and solution name as shown in the following Figure.

 

VB .NET Services and Types Program Example - creating a new console application project

 

Optionally you can change the Module name by changing the file name as shown in the Following Figure.

 

VB .NET Services and Types Program Example - renaming the source file

 

Add the following code.

 

Imports System.Net

 

Module TypesAndServices

    'This sample demonstrates some of the operations that

    'can be performed using the basic types and services provided in System.Net.

    Sub Main()

        Console.WriteLine("Do some testing...")

        Console.WriteLine("IPv4?")

        ParseIPAddress("87.248.113.14") 'IPv4 Address

        Console.WriteLine()

        Console.WriteLine("Ipv6?")

        ParseIPAddress("::1") 'IPv6 Address

        Console.WriteLine()

        Console.WriteLine("Invalid address...")

        ParseIPAddress("::1::2") 'Invalid Address

        Console.WriteLine()

        Console.WriteLine("Another invalid address...")

        ParseIPAddress("255.255.256.0") 'Invalid Address

        Console.WriteLine()

        Console.WriteLine("HTTP protected resource...")

        RequestProtectedResource("http://www.contoso.com/protected/resource.htm")

        Console.WriteLine()

        Console.WriteLine("FTP protected resources...")

        ' Change the URI accordingly...

        RequestProtectedResource("ftp://ftp.microsoft.com/Products/mspress/library/animat.zip")

        Console.WriteLine()

        Console.WriteLine("Resolving www.microsoft.com to IPs...")

        ResolveName("www.microsoft.com")

    End Sub

 

    Sub ResolveName(ByVal name As String)

        Try

            Dim hostInfo As IPHostEntry

            hostInfo = Dns.GetHostEntry(name)

 

            Dim address As IPAddress

            For Each address In hostInfo.AddressList

                Console.WriteLine(address.ToString)

            Next address

        Catch ex As Exception

            Console.WriteLine(ex.ToString)

        End Try

    End Sub

 

    Sub RequestProtectedResource(ByVal resource As String)

 

        Console.WriteLine("Please enter your user name.")

        Dim userName As String

        userName = Console.ReadLine()

 

        Console.WriteLine("Please enter your password.")

        Dim password As String

        password = Console.ReadLine()

 

        Dim client As New WebClient

 

        client.Credentials = New NetworkCredential(userName, password)

        client.DownloadFile(resource, "page.htm")

    End Sub

 

    Sub ParseIPAddress(ByVal addressString As String)

        Try

            Dim address As IPAddress

            address = IPAddress.Parse(addressString)

 

            Console.WriteLine("The address is " & address.ToString _

            & " and is of the family " & address.AddressFamily.ToString + ".")

        Catch ex As Exception

            Console.WriteLine("Failure parsing" & addressString & ". " & ex.ToString)

        End Try

    End Sub

End Module

 

Build and run the project (Start Without Debugging). The following is a sample output.

 

VB .NET Services and Types Program Example - a sample output with exception thrown

 

 

 


 

< Intro To System.net Namespace | Main | Socket-Level Classes >