Home About

 December 8th, 2008

Finding all local IP Addresses in C# - 1

A common problem is the find out the IP address of the machine your C# program is running on. C# provides two methods for obtaining the information. The first one is easy; but somewhat undocumented. The other one is just a little harder. If your code has to run on Mono then the second option is your only alternative.

Read the rest of this entry »

 December 3rd, 2008

Setting up Ubuntu for Web development - 0

One of the pleasures of setting up a new machine for development is always the feeling of having a “clean” machine without a myriad of test projects and things under development laying around the place. This article is distilled from my notes as I installed Apache2, MySQL & PHP5 on my new machine. And of course I tuned PHP5 by compiling and installing eaccelerator.

Installing Apache2 , MySQL Server and PHP5

The above has become so easy under Ubuntu that its quite unbelievable. With just a few lines of typing you too can have a full LAMP (Linux, Apache, MySQL & PHP) system. There is no need for the hours previously spend on hunting for dependencies to download and compile.

sudo apt-get install apache2
sudo apt-get install mysql-server
sudo apt-get install php5 php5-mysql php5-cli php5-dev
php -v

As a bonus: I also installed my favorite HTML editor: BlueFish

sudo apt-get install bluefish

And that is it really — everything works from here on. If you would like to get a bit more juice out of PHP5 you can also add the following:

Configure PHP5 with eaccelerator

PHP is a great language for building websites but it can be quite slow at times. Each time a script is run PHP needs to compile it and then execute it. You can gain speed by using a cache such as eaccelerator. This stores the compiled scripts in both memory and on disk so that frequently called scripts run much faster.

The following steps compile and install eacelerator 0.9.5.3, you can check for a newer version on the eacellerator website.

sudo apt-get install build-essential
cd /tmp
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
bzip2 -cd eaccelerator-0.9.5.3.tar.bz2 | tar xf -
cd eaccelerator-0.9.5.3
/usr/bin/phpize
./configure
make
sudo make install

After the above you should now have a file named eaccelerator.so in directory/usr/lib/php5/20060613+lfs/ (or whatever directory was printed when you did ‘make install’).

Now you need to configure php to include the eacelerator module on startup by editing the relevant php.ini file:

sudo gedit /etc/php5/apache2/php.ini

Add the following lines to the php.ini file:

; Installing the ZEND extension
zend_extension=”/usr/lib/php5/20060613+lfs/eaccelerator.so”
eaccelerator.shm_size=”0″
eaccelerator.cache_dir=”/var/local/eaccelerator”
eaccelerator.enable=”1″
eaccelerator.optimizer=”1″
eaccelerator.check_mtime=”1″
eaccelerator.debug=”0″
eaccelerator.shm_only=”0″
eaccelerator.compress=”1″
eaccelerator.compress_level=”9″
eaccelerator.allowed_admin_path=”/var/www/apache2-default/control.php”

Finally we need to create the cache directory used by eaccelerator to store the compiled scripts:

cd /var/local
sudo mkdir eaccelerator
sudo chown root:www-data /var/local/eaccelerator/
sudo chmod u=rwx,g=rwx,o= /var/local/eaccelerator/

Restart Apache2 and see if it loads correctly:

sudo apache2ctl configtest
sudo apache2ctl graceful

Test the PHP5 installation

Create a small file in “/var/www/” to test your PHP5 installation

sudo gedit /var/www/php5.php

And add the following in this file:

Now load http://localhost/php5.html to see if your system is correctly configured. If eaccelerator is properly configured it should also be listed here.

 November 27th, 2008

Setting up NUnit for C# Unit Testing with Visual Studio C# Express 2008 - 10

Unit testing helps you verify that each individual part of your code is working as expected and keeps doing that as you change your software. You do this by adding small bits of testing code and have the unit testing frame work execute them in order. I am currently writing some code that needs to have a basic unit testing framework and wanted to install the NUnit framework. This post is about how you can install NUnit, use it and run it with / install it for Visual Studio C# 2008 Express. I will leave the nuts and bolts of unit testing to a future post but this post should get you up and running.

Read the rest of this entry »

 November 26th, 2008

Building a simple portscanner in C# - 0

In the dark ages before the Internet there was “war-dialing”: randomly calling telephone numbers in the hope that on the other side a computer modem would pick up. War Dialing was glamorized by the movie “Wargames” but portscanning is just like that: you too can help the world narrowly avoid nuclear Armageddon. This article shows how you can build a simple Portscanner in C#.

Read the rest of this entry »

 November 26th, 2008

Converting an IP address to a Hostname, and back with C# - 1

If you are using TCP/IP connections in your C# application you will need to look up DNS addresses. The following article shows how you can convert an IP address to a hostname, and a hostname to an IP address. Other common uses are to find the hostname address of the local machine, and all its IP addresses — each of which we will show in the examples below.

Read the rest of this entry »

 November 25th, 2008

Encoding C# strings as Byte[] (Byte Arrays) and back again - 3

When working with io streams (such when sending and receiving information from a NetworkStream) you often have to convert C# strings into Byte[] (byte arrays) and back again. At this point it is important to consider how you would like to encode your string. This post shows how you can pass a string to a method that only accepts byte arrays — and how you can turn byte arrays back into strings again.
Read the rest of this entry »

 November 24th, 2008

Modifying the Windows Registry in C# - 0

One wrong move and your dead, or at least your computer. Nothing is more nerve wracking than manipulating the Windows Registry. In the following example we show how you can (safely!) create your own little part of the registry and store and retrieve your applications configuration. Which is a nice way to store user names, passwords and anything else that you fancy.

Read the rest of this entry »

 November 21st, 2008

Using Timers in C# - 0

Sometimes its handy to set an alarm clock — to tell you when to get that next cup of coffee for example. Your C# programs also might want to check that everything is going smoothly. Timers are handy if you want to check for example whether that download has stalled, a message needs to be send or a file needs to be updated.

Read the rest of this entry »

 November 21st, 2008

Using Semaphores in C# - 2

Semaphores allow us to synchronize threads inside a C# application. Semaphores are not used to transfer large amounts of data as are pipe’s or queues, instead they are intended to let multiple threads synchronize their actions. Typically you will have some kind of resource that only one , or a limited number of threads are allowed to access simultaneously.

Read the rest of this entry »

 November 20th, 2008

Using Named Pipes in C# / Windows - 1

Communication between different threads in a process is trivial as they share the same objects in memory. But what if you would like to communicate with a different process (program) on the same computer? You could open a TCP/IP port to share data but the added overhead of this would slow down your program if you send a lot of data.

Read the rest of this entry »


Recent Comments
  • Hakbor: This works fine. But to get the NUnit to use my current tests (and not the old ones) , it is not sufficient...
  • Alberto: Your plugin is very useful; I installed it on several different blogs I manage and I’m very happy with...
  • Nelson: Saved me from doing it myself. Good article.
  • andy: i am currently playing taiwanese server wow in 奈辛瓦里(PVP) and i would like to realm transfer to...
  • berties: any english speaking playing on a taiwanese server?