Fly on the Coconut and Source Code

I was processing the coconuts that the gardeners removed prior to Hurricane Ike. I was sitting outside on the patio, collecting the coconut water and the copra when I noticed that a fly was really working over a piece of coconut The fly stayed on the coconut for at least ten minutes. The lateral thinking side of my brain immediately envisaged a fly trap using coconut as the lure. I will race you to the patent office for this one.

I was also tickled by some lateral thinking in my C# programming. In our system, I have to transfer data by ftp back to our servers located in our money transfer location. To transfer the data, I have to be connected by wireless. The Personal Digital Assistant computer in this case connects to our wifi setups.

I cannot start the transfer until I know that I am connected. I could go through a whole bunch of code to see if I am connected, or I could take a lateral thinking approach. If the network card is connected wireless, that means that I have an IP address other than 127.0.01 which is the IP address for localhost when you are not connected. So with a few lines of code checking for an IP address of anything other than localhost address, I am off and away to the races. Here is the source code snippet:


///////////////////// *********** AM I WIFI ENABLED TEST METHOD
///
/// amIWiFiEnabled()
///

/// bool
/// signals that WiFi is enabled
/// does this by checking out dhcp. if the ip is anything
/// but 127.x.x.x then it is localhost and not connected to server
/// but if it is something different, then wifi is connect because the
/// network adapter is funtionally addressable with an IP address


public bool amIWiFiEnabled()
{
bool ret = false;
try
{
string hostName = Dns.GetHostName();
IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
string hostIPAdd = hostEntry.AddressList[0].ToString();
ret = hostIPAdd != IPAddress.Parse("127.0.0.1").ToString();
if (!ret)
{
try
{
// try turning wifi on
int crap = 1;
bool turn = iPAQSetWLANRadio(ref crap);
}
catch (Exception nn)
{
// dummy assignment
ErrorString = nn.Message;
}
}
}
catch
{
return false;
}
return ret;
}

In a future posting, I will tell you of the lateral thinking that I came up with involving rum and coconut and a new food product. I will race you to the patent office on that idea as well.

No comments: