Monday, January 28, 2013

Here is one last posting on finding oneself like Keira Knightley as Sabina Spielrein. Well, actually that is a terrible analogy.

Addendum 4/10/2013: A lot of this posting is really bad. See: this

Trilogy complete!
  1. this
  2. and this
  3. go with this...

Keira Knightley as Sabina Spielrein gives an excellent performance in David Cronenberg's film "A Dangerous Method" and yes, I think it the very best Cronenberg film, and I've seen every single one except for Spider, including the overrated Scanners. Naturally, Sabina Spielrein finding herself really doesn't have anything at all to do with finding yourself in C#...

string nameOfTheComputer = System.Net.Dns.GetHostName();
string localIp =
   System.Net.Dns.GetHostByName(nameOfTheComputer).AddressList[0].ToString();
string publicIpv4 = Request.ServerVariables["REMOTE_ADDR"];
string publicIpv6 =
   System.Net.Dns.GetHostEntry(nameOfTheComputer).AddressList[0].ToString();
string url = Request.Url.ToString();
string nameOfCurrentPage = Request.ServerVariables["SCRIPT_NAME"];
string localeInFileTree =
   Server.MapPath(System.IO.Path.GetFileName(nameOfCurrentPage));

 
 

Maybe VB script is more suitable, as it feels like it is eighty years old and thus from Sabrina's era...

Dim nameOfTheComputer As String = System.Net.Dns.GetHostName()
Dim localIp As String =
      System.Net.Dns.GetHostByName(nameOfTheComputer).AddressList(0).ToString()
Dim publicIpv4 As String = Request.ServerVariables("REMOTE_ADDR")
Dim publicIpv6 As String =
      System.Net.Dns.GetHostEntry(nameOfTheComputer).AddressList(0).ToString()
Dim url As String = Request.Url.ToString()
Dim nameOfCurrentPage As String = Request.ServerVariables("SCRIPT_NAME")
Dim localeInFileTree As String =
      Server.MapPath(System.IO.Path.GetFileName(nameOfCurrentPage))

 
 

Nah. The truth is, this is really bad analogy and I just like goofing off with Tumblr. So, what am I struggling to say?

Well, the stuff above will help you "find yourself." The variables are largely going to end up with stuff in them that corresponds appropriately to their names. One exception is that either the publicIpv4 or publicIpv6 variable is going to be botched in the code above, but I'll get to that in a minute. Other things to mention are:

  • The code above, in both cases, is from an MVC4 app, and, yes, you can make a VB script MVC4 app in case you're, well, crazy. (Hey, the analogy is starting to work again!) But you may need to replace everything that starts out with Request. with HttpContext.Current.Request. instead in some older versions of MVC or in web forms apps.
  • url will look like http://localhost:54197/ in an MVC4 app at the HomeController and nameOfCurrentPage returns a single forward slash.
  • Inspecting localeInFileTree by setting a breakpoint, I saw C:\\this\\that\\theotherthing in C# and C:\this\that\theotherthing in VB. Note that neither has a trailing slash.

 
 

Alright, the only thing really new about this posting in contrast to the other two is the IP stuff. First remember that a local IP starts with...

  1. 10,
  2. or 198.168,
  3. or 172.16,
  4. or 172.31,
  5. or anything between bullets three and four above.

 
 

publicIpv6 will end up with the same string as localIp (not literally the same... I do understand the heap and reference types) if there is not an IPv6 IP address to be had, which are those new freaky things that look like fe80::4869:3aca:f1b9:d214%10 for example. (The world is running out of the traditional IPv4 IP addresses the same way it is running out of tungsten. Those left not on the list above are public IP addresses.) So what will publicIpv4 look like?

It is likely going to look like ::1 if there is an IPv6 address and legitimate otherwise. Note that "legitimate" may include 127.0.0.1 which is not technically a private IP address. This means, that in order to really use the code above, one needs to do an equality comparison between publicIpv6 and localIp to see if either publicIpv4 or publicIpv6 holds the value for the public IP address. If publicIpv6 and localIp do match then publicIpv4 holds the public IP address while the absence of a match means that publicIpv6 holds the public IP address.

No comments:

Post a Comment