Monday, April 11, 2011

XmlNodeType Members

Enumeration XmlNodeType (namespace: System.Xml) is used to specify the types of XML node.
 
XmlNodeType members:
  • None  --> This is returned by the XmlReader if a Read method has not been called.
  • Element  --> An element (for example, ).
  • Attribute --> An attribute (for example, id='123' ).
  • Text  --> The text content of a node.
  • CDATA --> A CDATA section (for example, )
  • EntityReference --> A reference to an entity (for example, # ).
  • Entity  --> An entity declaration (for example, ).
  • ProcessingInstruction --> A processing instruction (for example, )
  • Comment  --> A comment (for example, )
  • Document  --> A document object that, as the root of the document tree, provides access to the entire XML document.
  • DocumentType --> for example,
  • Notation  --> for example,
  • Whitespace  --> White space between markup
  • SignificantWhitespace  --> White space between markup in a mixed content model or white space within the xml:space="preserve" scope
  • EndElement  --> for example,
  • EndEntity 
  • XmlDeclaration  --> for example,               
dd

Tuesday, April 5, 2011

Check IP Address Type

Idea:
- display all IP addresses
- inspect it one by one, whether:
  - it is IPv4
  - it is IPv6
     - it is loopback address
     - it is link local
     - it is multicast
     - it is site local
     - it is teredo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace ConsoleCekIPSatuSatu
{
    class Program
    {
        static IPAddress[] GetIPAddress(string host)
        {
            IPHostEntry hostInfo;
            hostInfo = Dns.GetHostEntry(host);
            return hostInfo.AddressList;
        }

        static void CheckIPType(IPAddress ip)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                Console.WriteLine("\t\tThis is IPv4");
                CheckIPv4(ip);
            }
            else
            {
                Console.WriteLine("\t\tThis is IPv6");
                CheckIPv6(ip);
            }
        }

        static void CheckIPv4(IPAddress ip)
        {
            // check whether it is loopback address or not
            if (IPAddress.IsLoopback(ip))
                Console.WriteLine("\t\tThis is IPv4 loopback address");
        }

        static void CheckIPv6(IPAddress ip)
        {
            // check whether it is loopback address or not
            if (IPAddress.IsLoopback(ip))
                Console.WriteLine("\t\tThis is IPv6 loopback address");

            // check whether it is link local address
            if (ip.IsIPv6LinkLocal)
                Console.WriteLine("\t\tThis is link local address");
            
            // check whether it is multicast address
            if(ip.IsIPv6Multicast)
                Console.WriteLine("\t\tThis is multicast address");

            // check whether it is site local address
            if (ip.IsIPv6SiteLocal)
                Console.WriteLine("\t\tThis is site local address");

            // check whether it is teredo address
            if (ip.IsIPv6Teredo)
                Console.WriteLine("\t\tThis is teredo address");

        }

        static void Main(string[] args)
        {
            string hostName = Dns.GetHostName();
            Console.WriteLine("Hostname: {0}", hostName);

            // get the IP addresses list
            IPAddress[] hostNameIPAddresses;
            hostNameIPAddresses = GetIPAddress(hostName);

            // check the type of each IP address
            foreach (IPAddress ip in hostNameIPAddresses)
            {
                Console.WriteLine("\n\t" + ip.ToString());
                CheckIPType(ip);
            }

            Console.ReadKey();
        }
    }
}



Monday, April 4, 2011

Resolving Hostname

I know, it is kind of lame. But I'll post it anyway.

using System;                   // for String and Console
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;               // for Dns, IpHostEntry, IPAddress
using System.Net.Sockets;       // for SocketException

namespace SocketCoba01
{
    class Program
    {
        static void PrintHostInfo(string host)
        {
            try
            {
                IPHostEntry hostInfo;

                hostInfo = Dns.GetHostEntry(host);  //karena Dns.resolve sudah obsolete 
                //(baca dokumentasi .NET)

                //display the primary hostname
                Console.WriteLine("\tCanonical Name: " + hostInfo.HostName);

                //display list of IP adresses for this host
                Console.WriteLine("\tIP Addresses: ");
                foreach (IPAddress ip in hostInfo.AddressList)
                {
                    Console.WriteLine("\t\t{0}", ip.ToString());
                }

                Console.WriteLine();

                //display list of all aliases for this host
                Console.WriteLine("\tAliases: ");
                foreach (String alias in hostInfo.Aliases)
                {
                    Console.WriteLine("\t\t{0}", alias);
                }
            }
            catch (Exception) 
            {
                Console.WriteLine(".:. Unable to resolve host: " + host + "\n");
            }
 
        }
        static void Main(string[] args)
        {
            try
            {
                String LocalHostName = Dns.GetHostName();
                Console.WriteLine("Local host");
                Console.WriteLine("\tHost name: {0}",LocalHostName);

                PrintHostInfo(LocalHostName);                
            }
            catch (Exception) 
            {
                Console.WriteLine("error @Main");
            }
            Console.ReadKey();
        }

       
    }
}