Carbon based life form

Wednesday, March 26, 2008

Dead wasps sting

My new baby daughter is giving us lessons in life everyday. But here's a new one, don't put a dead wasp in your mouth - they still can sting. Apart from a big fat lip she survived the experience.
I have to say that I admire something that will still fight back even when it is long dead.

Delving into sockets

The job is to receive data on port 80 and determine what URL it was targetting and then redirect the request to the appropriate server.

The purpose being that if you used a service like dyndns you could have lots of URLs but not have to register for lots of IP addresses. Yes, you could load up the one server with processes to respond to the requests but that is not scalable. Yes, you could setup a load balanced server farm, but that's a little too expensive.
Let's just have an internal network of cheap desktop PCs running the apps. The server has a permanent IP address, or could even have a dynamic IP. It is dual-homed with the second NIC connected to an internal switch.
Then you have a database of URL string and target IP combinations.
It is kind of like port forwarding with a little intelligent rerouting inbetween. I am calling it portSAF for port Split And Forward. I am sure that someone has already done this but I couldn't find it so here we go.
So the first step is to receive requests on port 80. To do that we need to use multithreading.
In the form_Load(){
....

alSockets = new ArrayList();
thdListener = new Thread(listenerThread);
thdListener.IsBackground = true;
thdListener.Start();
....
}
We need to set .IsBackground so that when the form closes the threads are terminated as well.

public void listenerThread()
{
Int32 port = 80;
TcpListener tcpListener = new TcpListener(IPAddress.Any, port);
tcpListener.Start();
for (; ; ){
if (tcpListener.Pending()){
Socket handlerSocket = tcpListener.AcceptSocket();
if (handlerSocket.Connected){
lock (this)
{
alSockets.Add(handlerSocket);
}
Thread thdHandler = new Thread(handlerThread);
// When the foreground thread terminates so should this one.
thdHandler.IsBackground = true;
thdHandler.Start();
}
}
Thread.Sleep(10);
}
}
The listenerThread method listens for new connections, when one is detected a socket is created and put on an array. Then a new thread is started it will use the last socket from the array.

The use of Thread.Sleep() was to stop the CPU usage gonig to 99%

Tuesday, March 4, 2008

Starting fresh

I am finally blogging.

Feeling better connected already.

Thus begins a new phase for me.
Baby has arrived
Starting an Aquaponics setup.
VFP is done, now the challenge begins to evolve the most complex software project I have ever been involved with over to .Net. The question is how to continue with the current products but also have them blending .Net so that eventually it will all be .Net. That will be the first challenge.

C# will be the language of choice. Check out C# vs VB jobs in Canberra on seek.com.au. 110 vs 31 (as of the last time I checked). Apart from that though the only language that will work on the micro Framework platform is C#. The micro framework is targetted at devices like this one:
http://www.dominion.net.au/index.php?a=&b=&c=&d=DC-ME-MF which is the direction I am going for control of the Aquaponics stuff.
I actually started relearning C++/CLI only to discover that it is not supported on the compact framework. Besides I have to say, and the gurus will disagree, but the MS implementation of C++ is becoming a dogs breakfast. I am sure that there are plenty of valid reasons for it being they way it is but learning it now is just too hard. Just trying to figure out how strings are represented on different platforms has lead me to believe that life is too short for large scale C++ development.
VB is fine but I really prefer a more compact language. So C# it is.
So I am going to begin retraining myself. I am geographically remote from learning institutions and I have serious doubts that they can keep up with technology anyway. So I am going to train myself and here is where the results will go.

Labels: