HttpListener handling multiple requests

I have this HttpListener, which works perfect for a single requesst, but then it shuts down after finishing up the request. What I'm interested in, is a listener that keeps up the connection with the client until there's no more files in the specified URL. I've tried fiddling around with threads and asynchronous calls, but I haven't been able to make anything of it working thus far. I just have a hard time imagining there isn't some relatively easy way to get a HttpListener to keep up the connection instead of shutting down after completing each request.

public static void Listener(string[] prefixes)
    {
        if (!HttpListener.IsSupported)
        {
            Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
            return;
        }
        // URI prefixes are required, 
        // for example "http://contoso.com:8080/index/".
        if (prefixes == null || prefixes.Length == 0)
            throw new ArgumentException("prefixes");


        // Create a listener.
        HttpListener listener = new HttpListener();
        // Add the prefixes. 
        foreach (string s in prefixes)
        {
            listener.Prefixes.Add("http://" + s + "/");
        }


        listener.Start();
        Console.WriteLine("\nListening...");

        HttpListenerContext context = listener.GetContext();
        Console.WriteLine("Request received...\n");

        HttpListenerRequest request = context.Request;

        // Obtain a response object.
        string url = context.Request.RawUrl;

        string[] split = url.Split('/');

        int lastIndex = split.Length - 1;

        int x, y, z;

        x = Convert.ToInt32(split[lastIndex]);
        y = Convert.ToInt32(split[lastIndex - 1]);
        z = Convert.ToInt32(split[lastIndex - 2]);

        HttpListenerResponse response = context.Response;


        #region Load image and respond

        // Load the image
        Bitmap bm = new Bitmap("C:\\MyFolder\\image_1\\");
        MemoryStream bmStream = new MemoryStream();
        bm.Save(bmStream, ImageFormat.Png);
        byte[] buffer = bmStream.ToArray();

        // Get a response stream and write the response to it.
        response.ContentLength64 = bmStream.Length;
        response.ContentType = "image/png";
        response.KeepAlive = true;
        System.IO.Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);

        // You must close the output stream.
        output.Close();
        listener.Stop();

        #endregion

And here's the Program:

    class Program
{
    static void Main(string[] args)
    {
        string name = (args.Length < 1) ? Dns.GetHostName() : args[0];
        try
        {   //Find the IPv4 address 
            IPAddress[] addrs = Array.FindAll(Dns.GetHostEntry(string.Empty).AddressList,
                a => a.AddressFamily == AddressFamily.InterNetwork);
            Console.WriteLine("Your IP address is: ");
            foreach (IPAddress addr in addrs)
                Console.WriteLine("{0} {1}", name, addr);

            //Automatically set the IP address
            string[] ips = addrs.Select(ip => ip.ToString()).ToArray();
            Response.Listener(ips);

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        //Manually setting the IP - not optimal!
        //string[] ipstring = new string[1] { "10.10.180.11:8080" };
        //Response.Listener(ipstring);


    }

}
Jon Skeet
people
quotationmark

Yes - you're calling GetContext once, serving that request, then stopping.

Instead, you should be calling GetContext in a loop. Depending on whether you want to be able to handle multiple requests concurrently or not, you might have GetContext in one thread, and then hand off each request to a separate (probably thread-pool) thread to respond to it.

The slightly tricky bit is shutting down - if you want a clean shutdown, you'll need to work out when to stop the loop (and what to do if you're in the middle of a GetContext call), and wait for outstanding requests to complete.

people

See more on this question at Stackoverflow