11
|
1 using System;
|
|
2 using System.Net;
|
|
3
|
|
4 class ParseAddress
|
|
5 {
|
|
6
|
|
7 private static void Main(string[] args)
|
|
8 {
|
|
9 string IPaddress;
|
|
10
|
|
11 if (args.Length == 0)
|
|
12 {
|
|
13 Console.WriteLine("Please enter an IP address.");
|
|
14 Console.WriteLine("Usage: >cs_parse any IPv4 or IPv6 address.");
|
|
15 Console.WriteLine("Example: >cs_parse 127.0.0.1");
|
|
16 Console.WriteLine("Example: >cs_parse 0:0:0:0:0:0:0:1");
|
|
17 return;
|
|
18 }
|
|
19 else
|
|
20 {
|
|
21 IPaddress = args[0];
|
|
22 }
|
|
23
|
|
24 // Get the list of the IPv6 addresses associated with the requested host.
|
|
25 Parse(IPaddress);
|
|
26 }
|
|
27
|
|
28 // This method calls the IPAddress.Parse method to check the ipAddress
|
|
29 // input string. If the ipAddress argument represents a syntatically correct IPv4 or
|
|
30 // IPv6 address, the method displays the Parse output into quad-notation or
|
|
31 // colon-hexadecimal notation, respectively. Otherwise, it displays an
|
|
32 // error message.
|
|
33 private static void Parse(string ipAddress)
|
|
34 {
|
|
35 try
|
|
36 {
|
|
37 // Create an instance of IPAddress for the specified address string (in
|
|
38 // dotted-quad, or colon-hexadecimal notation).
|
|
39 IPAddress address = IPAddress.Parse(ipAddress);
|
|
40
|
|
41 // Display the address in standard notation.
|
|
42 Console.WriteLine("Parsing your input string: " + "\"" + ipAddress + "\"" + " produces this address (shown in its standard notation): "+ address.ToString());
|
|
43 }
|
|
44
|
|
45 catch(ArgumentNullException e)
|
|
46 {
|
|
47 Console.WriteLine("ArgumentNullException caught!!!");
|
|
48 Console.WriteLine("Source : " + e.Source);
|
|
49 Console.WriteLine("Message : " + e.Message);
|
|
50 }
|
|
51
|
|
52 catch(FormatException e)
|
|
53 {
|
|
54 Console.WriteLine("FormatException caught!!!");
|
|
55 Console.WriteLine("Source : " + e.Source);
|
|
56 Console.WriteLine("Message : " + e.Message);
|
|
57 }
|
|
58
|
|
59 catch(Exception e)
|
|
60 {
|
|
61 Console.WriteLine("Exception caught!!!");
|
|
62 Console.WriteLine("Source : " + e.Source);
|
|
63 Console.WriteLine("Message : " + e.Message);
|
|
64 }
|
|
65 }
|
|
66 } |