From 1aa5c6d948957947519fc04e4a878a7571d73d4c Mon Sep 17 00:00:00 2001 From: Ethan O'Brien Date: Mon, 15 Aug 2022 14:56:48 -0500 Subject: [PATCH] fix high cpu bug --- Form1.cs | 8 ++++---- Program.cs | 12 ++++++++---- version | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Form1.cs b/Form1.cs index c9bfe99..33ca987 100644 --- a/Form1.cs +++ b/Form1.cs @@ -7,7 +7,7 @@ namespace WebServer { public partial class WebServer : Form { - private double version = 2.7; + private double version = 2.8; Server mainServer; private string pathToServe; private int port = 8080; @@ -55,7 +55,7 @@ private void startServer() { mainServer = new Server(pathToServe, port, PUT.Checked, DELETE.Checked, CORS.Checked, AutoIndex.Checked, ListDirectory.Checked, localNetwork.Checked); } - private Label[] URLS = new Label[5] { null, null, null, null, null}; + private Label[] URLS = new Label[5] { null, null, null, null, null }; private void SetURLText() { for (int i = 0; i < URLS.Length; i++) @@ -80,10 +80,10 @@ private void SetURLText() URLS[i].Font = new System.Drawing.Font("Microsoft Sans Serif", 10F); URLS[i].ForeColor = System.Drawing.SystemColors.Highlight; URLS[i].Location = new System.Drawing.Point(80, location); - URLS[i].Name = "URL"+i; + URLS[i].Name = "URL" + i; URLS[i].Size = new System.Drawing.Size(276, 17); URLS[i].TabIndex = 14; - URLS[i].Text = "http://"+urls[i]+":"+this.port+"/"; + URLS[i].Text = "http://" + urls[i] + ":" + this.port + "/"; URLS[i].Click += new System.EventHandler(this.URL_Click); this.Controls.Add(URLS[i]); location += 30; diff --git a/Program.cs b/Program.cs index 89ad0cb..5a9ce29 100644 --- a/Program.cs +++ b/Program.cs @@ -202,7 +202,7 @@ public string[] GetURLs() rv[0] = "127.0.0.1"; for (int i = 0, j = 1; i < addr.Length; i++) { - if (addr[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) + if (addr[i].AddressFamily == AddressFamily.InterNetwork) { rv[j] = addr[i].ToString(); j++; @@ -228,9 +228,12 @@ private void onRequest(Object obj) { byte[] bytes = new byte[1]; int bytesRec = handler.Receive(bytes); + if (bytesRec == 0) break; data += Encoding.UTF8.GetString(bytes, 0, bytesRec); consumed = (data.IndexOf("\r\n\r\n") != -1); } + if (!consumed) + break; //Console.WriteLine("Text received : {0}", data); string url = Uri.UnescapeDataString(data.Split(' ')[1].Split('?')[0]); string method = data.Split(' ')[0]; @@ -279,10 +282,10 @@ private void onRequest(Object obj) } catch (Exception e) { - handler.Shutdown(SocketShutdown.Both); - handler.Close(); Console.WriteLine("Error: {0}", e.ToString()); } + handler.Shutdown(SocketShutdown.Both); + handler.Close(); } private void writeHeader(Socket handler, int httpCode, string code, long cl, string ct, string extra) { @@ -502,7 +505,8 @@ private void put(Socket handler, string path, string data) } written += (long)a; byte[] bytes = new byte[a]; - handler.Receive(bytes); + int qwe = handler.Receive(bytes); + if (qwe == 0) break; fs.Write(bytes, 0, a); } fs.Close(); diff --git a/version b/version index e6b7b62..af65e02 100644 --- a/version +++ b/version @@ -1 +1 @@ -2.7 \ No newline at end of file +2.8 \ No newline at end of file