1

I'm trying to open a connection in C# to the server through 5 or 6 tabs, simultaneously.

The first connection works.

The others hang (the browser is waiting)

public class HtServer {
    HttpListener HL = new HttpListener();

    public void startServer(){
        HL.Prefixes.Add("http://127.0.0.1:800/");
        HL.Start();
        IAsyncResult HLC = HL.BeginGetContext(new AsyncCallback(clientConnection),HL);
    }

    public static void clientConnection(IAsyncResult res){
        HttpListener listener = (HttpListener)res.AsyncState;
        HttpListenerContext context = listener.EndGetContext(res);
        HttpListenerRequest request = context.Request;
        // Obtain a response object.
        HttpListenerResponse response = context.Response;
        // Construct a response. 
        string responseString = "<HTML><BODY> Hello world! " + DateTime.Now + "</BODY></HTML>";
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
        // Get a response stream and write the response to it.
        response.ContentLength64 = buffer.Length;
        System.IO.Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);
        // You must close the output stream.
        Thread.Sleep(4000);
        output.Close();
    }
}
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
yossi
  • 3,090
  • 7
  • 45
  • 65

0 Answers0