// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; public class SlowServer { private const int SIZE = 1000000; private const double SPEED = 10000; private const string LINE = "123456789012345678"; private const int CHUNK = 100; public static void Main(string[] args) { TcpListener server = new TcpListener(IPAddress.Any, 80); server.Start(); while(true) { try { TcpClient client = server.AcceptTcpClient(); StreamReader rdr = new StreamReader(client.GetStream()); StreamWriter wrt = new StreamWriter(client.GetStream()); string line; while((line = rdr.ReadLine()) != "") { Console.WriteLine(line); } wrt.WriteLine("HTTP/1.0 200 OK"); wrt.WriteLine("Content-Length: " + SIZE); wrt.WriteLine("Content-Type: text/plain"); wrt.WriteLine("Content-Disposition: attachment; filename=\"test.txt\""); wrt.WriteLine(); wrt.Flush(); int bytes = CHUNK * (LINE.Length + 2); for(int i = 0; i < SIZE / bytes; i++) { long t1 = DateTime.Now.Ticks; for(int j = 0; j < CHUNK; j++) { wrt.WriteLine(LINE); } long t2 = DateTime.Now.Ticks; double secs = (t2 - t1) / 10000000.0; if(bytes / secs > SPEED) { double wait = bytes / SPEED - secs; Thread.Sleep((int)(1000 * wait)); } wrt.Flush(); } rdr.Close(); wrt.Close(); client.Close(); } catch(Exception e) { Console.WriteLine(e); } } server.Stop(); } }