Saturday, June 4, 2011

Turn Your PC Into a Web Server

You can enable your PC (desktop or laptop) to be a web server to host a web site, available to anyone on the Internet. You just need to know the ip address of your PC. If you are connected directly to the Internet, you can run ipconfig from the command prompt and it will be displayed similar to this:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.1.5
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1

If you are connected via a router, you can go to a site like http://www.whatismyip.com/ and it will be displayed for you similar to this:

What Is My IP Address - WhatIsMyIP.com
Your IP Address Is: 14.254



Why are they different?
Sometimes they're not. If your computer is connected directly to the Internet, then your machine is assigned an Internet IP address. The IP address you see using ipconfig will be the same as that which you see reported by the "what's my IP" sites. If you're behind a router then the Internet IP address is actually assigned to the router. The router then hands out "local" IP addresses to the individual machines connected to it. Those are the "192.168." addresses you'll commonly see when running ipconfig.

So if your behind a router...
You will have to use Port Forwarding to reach your PC behind the router. Here is an example.

How to find your website.
If your router does not have a static ip address, the address will change every time it is restarted. So if you have people trying to access your website, you can't give them a static ip as it will change. Here are two variations of a solution that I use (if you have other ideas, please let me know, I'd be interested).

First, I have my web server "publish" it's location. You can use something elaborate as a messaging service, a field in a SQL Server database or a simple as a line in a text file.

I use something like the C# code below programatically find the routers ip:

private void GetNetworkConfig()
{
ManagementClass mc = new        
      ManagementClass("Win32_NetworkAdapterConfiguration");

    ManagementObjectCollection nics = mc.GetInstances();
    foreach (ManagementObject nic in nics)
    {
        if (Convert.ToBoolean(nic["ipEnabled"]) == true)
        {
            string IpAddress = (nic["IPAddress"] as String[])[0];
            string IPSubnet = (nic["IPSubnet"] as String[])[0];
            string DefaultGateWay = (nic["DefaultIPGateway"] as String[])[0];
        }
    }
}

then publish it to a text file on the Internet where either:
1) A web site that I do own (i.e. Estrellasoft.com) will do a REDIRECT from the page it is on to the web server behind the router, but this will display the ip address (and port if used) and it will look less professional.
2) I use frames or Iframes and set the target of the main frame page to the web server behind the router but the URL with the domain name (i.e. Estrellasoft.com) is still displayed as if the content was hosted there on that Internet page. It will look like the page rendered by the web server behind the router is being hosted on the Internet when it is actually sitting on a desk in your office. Great for hosting applications that you may own (Crystal Reports, Dundas Charts) that and ISP may charge you an arm and a leg for, assuming your ISP does any app hosting at all.

What is REALLY SOA?

According to Wikipedia, Service Oriented Architecture (SOA) is a software architecture where functionality is grouped around business processes and packages as interoperable services. But search the web and you are probably going to find a number of different definitions and implementations. Dr. Jim Webber, the Global Architecture lead for ThoughtWorks and co-author of the book "Developing Enterprise Web Services - An Architect's Guide", explains this in and an interview on the .Net Rocks Internet radio show (listen here).

I personally have seen SOA implementations that were geared to .Net applications as consumers because the messages were DataSets which although serialize nicely, may be a problem for a Java application to understand. One of the tenets of SOA (the way I understand it) is supposed to be interoperability: platform, language and protocol agnostic. Otherwise you just have a bunch of web services. So are those implementations REALLY SOA?

What's All This Then???

To Paraphrase William Glasser's quote:
"You Remember
10% of what we read
70% of what we discuss
80% of what we experience
95% of what we teach others.”
So as a way to retain and also to share with others I will post some weird and/or unusual of what I've learned about programming, mostly C#, but some VB, C++ and Java as well.