>> Or spawn a listener thread and let that block.
What do you mean here?
Never mind blocking. This is how you do it.
This code is excerpted from a custom control I wrote for an embedded
webserver. I use it to fulfil requests for addition resources like CSS or
images when I use an embedded webserver to render HTML. You can make it pick
a random port, and the widget exposes a LocalBaseUrl property that tells
your app the base URL for connecting to itself. That's what baseURL is
about.
Sockets aren't that hard but it takes a mind that's twisted in a special
way.
THIS CODE IS COPYRIGHT TO ME AND ANYONE PLANNING ON USING IT IN A
COMMERCIAL APPLICATION CAN JOLLY WELL PUBLICLY CREDIT ME.
The next Indian contractor to send a peremptory "UGENT: PLS HELP" message to
my private email address will receive pictures of dead cows in his work
inbox unless the request is accompanied by an offer to pay for my time and
trouble.
....
private TcpListener tcpListener;
private bool useRandomPort = true;
private int tcpListenerPort = 8080;
private bool shouldListen = false;
private Thread ListenerThread;
private string baseURL;
....
public void Start() {
if (useRandomPort)
tcpListenerPort = (new Random()).Next( 16384,65535);
baseURL = "http://localhost:" + tcpListenerPort + "/";
tcpListener = new TcpListener(IPA ddress.Loopback ,tcpListenerPor t);
if ((VirtualRoots. Count>0) && (null==filesys) )
filesys = new WebResourceProv iderFileSystem( );
shouldListen = true;
tcpListener.Sta rt();
ListenerThread = new Thread(new ThreadStart(Lis ten));
ListenerThread. Priority = ThreadPriority. BelowNormal;
ListenerThread. Name = string.Format(" {0} HTTP
Listener",this. GetType().ToStr ing());
ListenerThread. Start();
}
public void Stop() {
shouldListen = false;
}
....
private void Listen(){
while (shouldListen) {
if (tcpListener.Pe nding()){
Socket socket = tcpListener.Acc eptSocket();
socket.Blocking = false;
//Some applications disconnect immediately when just checking
//the continued presence of this app. These produce SelectError.
if (!socket.Poll(5 000,SelectMode. SelectError)) {
//spawn another thread to handle this request
HttpRequestHand ler handler = new
HttpRequestHand ler(this,socket );
Thread newThread = new Thread(new ThreadStart(han dler.Exec));
newThread.Name = string.Format(" {0} request
handler",this.G etType().ToStri ng());
newThread.Start ();
}
} else
Thread.Sleep(20 0);
}
}