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%
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%
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home