Skip to content

Instantly share code, notes, and snippets.

@beyondlimits
Last active June 22, 2017 17:51
Show Gist options
  • Save beyondlimits/92f8b3698fed5cb33f12ffd41bb3c14b to your computer and use it in GitHub Desktop.
Save beyondlimits/92f8b3698fed5cb33f12ffd41bb3c14b to your computer and use it in GitHub Desktop.
/*
This is extremely basic HTTP server.
Thing is that I don't want to provide Content-Length.
The problem is that when I close the connection from server side,
Firefox and curl most the times claim the connection is broken.
Without closing the connection they're stuck.
What I'm doing wrong?
*/
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
class Program
{
public static void Main()
{
TcpListener listener = new TcpListener(IPAddress.Loopback, 80);
listener.Start();
for(;;) {
TcpClient client = listener.AcceptTcpClient();
Thread thread = new Thread(parameter => {
Stream stream = ((TcpClient)parameter).GetStream();
StringBuilder sb = new StringBuilder();
sb.AppendLine("HTTP/1.1 200 OK");
sb.AppendLine("");
sb.AppendLine("OK");
byte[] buf = Encoding.UTF8.GetBytes(sb.ToString());
stream.Write(buf, 0, buf.Length);
stream.Close();
});
thread.Start(client);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment