Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. // This program shows how to use the IPAddress class to obtain a server
  2. // IP addressess and related information.
  3.  
  4. using System;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace Mssc.Services.ConnectionManagement
  10. {
  11.  
  12. class TestIPAddress
  13. {
  14.  
  15. /**
  16. * The IPAddresses method obtains the selected server IP address information.
  17. * It then displays the type of address family supported by the server and its
  18. * IP address in standard and byte format.
  19. **/
  20. private static void IPAddresses(string server)
  21. {
  22. try
  23. {
  24. System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
  25.  
  26. // Get server related information.
  27. IPHostEntry heserver = Dns.GetHostEntry(server);
  28.  
  29. // Loop on the AddressList
  30. foreach (IPAddress curAdd in heserver.AddressList)
  31. {
  32.  
  33.  
  34. // Display the type of address family supported by the server. If the
  35. // server is IPv6-enabled this value is: InternNetworkV6. If the server
  36. // is also IPv4-enabled there will be an additional value of InterNetwork.
  37. Console.WriteLine("AddressFamily: " + curAdd.AddressFamily.ToString());
  38.  
  39. // Display the ScopeId property in case of IPV6 addresses.
  40. if(curAdd.AddressFamily.ToString() == ProtocolFamily.InterNetworkV6.ToString())
  41. Console.WriteLine("Scope Id: " + curAdd.ScopeId.ToString());
  42.  
  43.  
  44. // Display the server IP address in the standard format. In
  45. // IPv4 the format will be dotted-quad notation, in IPv6 it will be
  46. // in in colon-hexadecimal notation.
  47. Console.WriteLine("Address: " + curAdd.ToString());
  48.  
  49. // Display the server IP address in byte format.
  50. Console.Write("AddressBytes: ");
  51.  
  52.  
  53.  
  54. Byte[] bytes = curAdd.GetAddressBytes();
  55. for (int i = 0; i < bytes.Length; i++)
  56. {
  57. Console.Write(bytes[i]);
  58. }
  59.  
  60. Console.WriteLine("\r\n");
  61.  
  62. }
  63.  
  64. }
  65. catch (Exception e)
  66. {
  67. Console.WriteLine("[DoResolve] Exception: " + e.ToString());
  68. }
  69. }
  70.  
  71. // This IPAddressAdditionalInfo displays additional server address information.
  72. private static void IPAddressAdditionalInfo()
  73. {
  74. try
  75. {
  76. // Display the flags that show if the server supports IPv4 or IPv6
  77. // address schemas.
  78. Console.WriteLine("\r\nSupportsIPv4: " + Socket.SupportsIPv4);
  79. Console.WriteLine("SupportsIPv6: " + Socket.SupportsIPv6);
  80.  
  81. if (Socket.SupportsIPv6)
  82. {
  83. // Display the server Any address. This IP address indicates that the server
  84. // should listen for client activity on all network interfaces.
  85. Console.WriteLine("\r\nIPv6Any: " + IPAddress.IPv6Any.ToString());
  86.  
  87. // Display the server loopback address.
  88. Console.WriteLine("IPv6Loopback: " + IPAddress.IPv6Loopback.ToString());
  89.  
  90. // Used during autoconfiguration first phase.
  91. Console.WriteLine("IPv6None: " + IPAddress.IPv6None.ToString());
  92.  
  93. Console.WriteLine("IsLoopback(IPv6Loopback): " + IPAddress.IsLoopback(IPAddress.IPv6Loopback));
  94. }
  95. Console.WriteLine("IsLoopback(Loopback): " + IPAddress.IsLoopback(IPAddress.Loopback));
  96. }
  97. catch (Exception e)
  98. {
  99. Console.WriteLine("[IPAddresses] Exception: " + e.ToString());
  100. }
  101. }
  102.  
  103.  
  104. public static void Main(string[] args)
  105. {
  106. string server = null;
  107.  
  108. // Define a regular expression to parse user's input.
  109. // This is a security check. It allows only
  110. // alphanumeric input string between 2 to 40 character long.
  111. Regex rex = new Regex(@"^[a-zA-Z]\w{1,39}$");
  112.  
  113. if (args.Length < 1)
  114. {
  115. // If no server name is passed as an argument to this program, use the current
  116. // server name as default.
  117. server = Dns.GetHostName();
  118. Console.WriteLine("Using current host: " + server);
  119. }
  120. else
  121. {
  122. server = args[0];
  123. if (!(rex.Match(server)).Success)
  124. {
  125. Console.WriteLine("Input string format not allowed.");
  126. return;
  127. }
  128. }
  129.  
  130. // Get the list of the addresses associated with the requested server.
  131. IPAddresses(server);
  132.  
  133. // Get additonal address information.
  134. IPAddressAdditionalInfo();
  135. }
  136.  
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement