Monday, July 20, 2009

converting IP address to IP number

Ip number is a numerical representation of the ip address we are all used to .
For instance 1.2.3.4 = 1690960 ;

It is good for some reasons, like conveting the Ip address to geographical position, or rule manupulation like firewalls and network devices.

How do you do that ?

Here is a little method that shows how to do that in c #

public static uint convertIP(string sIp)
{
string[] sOctecs;
char[] cSeperator = { '.' };
int[] iOctet = new int[4];
uint iReturnIPNumber = 0;
if (sIp != "")
{
bool bConvertOK;
sOctecs = sIp.Split(cSeperator, 4);//split the string to the octets
for (int i = 0; i <>
{
bConvertOK = int.TryParse(sOctecs[i], out iOctet[i]);
if (!bConvertOK)
{
return 0;
}
}//end for
// iOctecs contain integers , lets do the magic

//some math
iReturnIPNumber = (uint)iOctet[3];
iReturnIPNumber = iReturnIPNumber + ((uint)iOctet[2] * (uint)256);
iReturnIPNumber = iReturnIPNumber + ((uint)iOctet[1] * (uint)256 * (uint)256);
iReturnIPNumber = iReturnIPNumber + ((uint)iOctet[0] * (uint)256 * (uint)256 * (uint)256);

}
return iReturnIPNumber;
}


If you need it in the database , then mySQL has a built in functions of converting

mysql> SELECT INET_ATON('192.168.10.50');
+----------------------------+
| INET_ATON('192.168.10.50') |
+----------------------------+
| 3232238130 |
+----------------------------+
1 row in set (0.00 sec)

mysql> SELECT INET_NTOA(839559360);
+----------------------+
| INET_NTOA(839559360) |
+----------------------+
| 50.10.168.192 |
+----------------------+
1 row in set (0.00 sec)

Thursday, July 16, 2009

Vista Speech Regognition API

This will teach your program to read text from the voice it "hears" using .NET 3

so this is how it goes :
  1. Go to the references of your project and add System.Speech.Recognition
  2. Create a new instance of the SpeechRecognitionEngine
    SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new CultureInfo("en-US"));
  3. Set a file to read from and a dictionary to use:
    sre.SetInputToWaveFile("c:\\test.wav");
    sre.LoadGrammar(new DictationGrammar());
  4. Now read
    RecognitionResult rr = sre.Recognize();
  5. If something fails inside the Recognize() then it returns null , otherwise rr.Text will get you the text you are looking for ;
now the problem is and it stays unsolved , that out of the box , the engine does not usually hit the correct text . Good luck and mail me if you have ideas


SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new CultureInfo("en-US"));
sre.SetInputToWaveFile("c:\\test.wav");
sre.LoadGrammar(new DictationGrammar());
RecognitionResult rr = sre.Recognize();
if (rr != null)
{
Console.WriteLine(rr.Text);
}
else
{
Console.WriteLine("Recognition failed");
}
Console.ReadKey();



Sunday, July 12, 2009

Walkthrough: Creating a Windows Service Application in the Component Designer

The full step-by-stem straightforward guide from MSDN to create a Windows Service, all with Eventlog registration, action handling, Setup Project, installation and testing.

http://msdn.microsoft.com/en-us/library/zt39148a.aspx

For all your Visual Studio service-creation needs!

Wednesday, July 8, 2009

Semicolon after the if statement

Too frequent mistake to stay quiet about : the semicolon ";" after the if statement

consider the code

if (booleanMethod(s1,s2));
{
return true; // will be executed anyway
}

The code will return true in every case , because there is a semicolon after the if statement. The problem lays in the definition of the semicolon in modern languages such as C# and JAVA semicolon defines the ending of the statement and since the If statement doen't have to be followed by any other statement the true meaning of the upper code is :
if booleanMethod is true
do - nothing;
return true;

so if we want the code to return true only if booleanMethod is true the correct form would be without the semicolon after the if

if (booleanMethod(s1,s2))
{
// only if the if is true
return true;
}

Tuesday, July 7, 2009

Copying Microsoft SQL table to MySQL


Performing and Performing once again; today, we need much quicker database access than we currently have: we have some Windows servers contacting with the sql database using Microsoft's SQLClient driver for accessing it.

The idea was to cache some frequently used data on the servers in the in memory databases. The problem was copying the tables' schema from the sql server to the mysql without doing in manually. So:

I wanted to recommend the Michael Kofler software . I used the gui versions and had only one problem : they both require the correct MySQL ODBC driver. I used the last version located at http://dev.mysql.com/downloads/connector/odbc/3.51.html#winx64 and nothing worked , because the MySql server version installed was not 64 bit (what a pitty ) therefore, reinstalling the Windows MSI Installer (x86) version solved the issue and the table had been copyed in notime

Thanks Micael

MySQL Memory Table Size

If you're doing some Real Time processing with MySQL, you probably better use a Memory table and only eventually commit the results to a disk table.

But if you do that with the default setting, you'll soon run out of memory on a 16MB table!!

Don't worry, it is configurable!
http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_max_heap_table_size
(Note: this refers to MySQL 5.1 version, you may have another version. Also see page for other system configuration options)

And to make a long story short, open MySQL Administrator --> Health --> System Variables --> Memory --> max_heap_table_size
and set it to 268435456 (=256MB)