Skip to content

Commit 441d6f0

Browse files
committed
bug fixes, start on load
1 parent d7ca5aa commit 441d6f0

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

Form1.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ namespace WebServer
77
{
88
public partial class WebServer : Form
99
{
10-
private double version = 2.4;
10+
private double version = 2.6;
1111
Server mainServer;
1212
private string pathToServe;
1313
private int port = 8080;
1414
private bool running = false;
15-
private string localHostURL = "http://localhost:8080/";
15+
private string localHostURL = "http://127.0.0.1:8080/";
16+
private bool actionInProgress = false;
1617
public WebServer()
1718
{
1819
InitializeComponent();
@@ -26,18 +27,24 @@ public WebServer()
2627
this.UpdateVersion.Visible = true;
2728
this.UpdateVersion.Text = "Version " + version + " is out!";
2829
});
30+
this.running = true;
31+
actionInProgress = true;
32+
toggleServer();
33+
actionInProgress = false;
2934
}
3035
private void toggleServer()
3136
{
3237
if (this.running)
3338
{
39+
this.Status.Text = "Running";
3440
startServer();
3541
}
3642
else if (mainServer != null)
3743
{
3844
mainServer.Terminate();
3945
mainServer = null;
4046
this.URL.Visible = false;
47+
this.Status.Text = "Not Running";
4148
this.MSG.Text = "Not Running";
4249
this.MSG.Visible = true;
4350
}
@@ -47,7 +54,7 @@ private void startServer()
4754
this.MSG.Visible = false;
4855
this.URL.Visible = true;
4956
mainServer = new Server(pathToServe, port, PUT.Checked, DELETE.Checked, CORS.Checked, AutoIndex.Checked, ListDirectory.Checked);
50-
this.localHostURL = "http://localhost:" + port + "/";
57+
this.localHostURL = "http://127.0.0.1:" + port + "/";
5158
this.URL.Text = "Open " + this.localHostURL + " in your browser";
5259
}
5360
private void saveSettings()
@@ -122,19 +129,17 @@ private void loadSettings()
122129
}
123130
this.ServingPath.Text = "Currently Serving: " + pathToServe;
124131
this.Port.Value = new decimal(new int[] { port, 0, 0, 0 });
125-
this.localHostURL = "http://localhost:" + port + "/";
132+
this.localHostURL = "http://127.0.0.1:" + port + "/";
126133
this.URL.Text = "Open " + this.localHostURL + " in your browser";
127134
}
128135
catch (Exception e) { }
129136
}
130-
bool actionInProgress = false;
131137
private void button1_Click(object sender, EventArgs e)
132138
{
133139
if (actionInProgress) return;
134140
actionInProgress = true;
135141
running = !running;
136142
toggleServer();
137-
this.Status.Text = running ? "Running" : "Not Running";
138143
actionInProgress = false;
139144
}
140145

Program.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,27 @@ private void ServerMain()
157157
{
158158
try
159159
{
160-
IPHostEntry host = Dns.GetHostEntry("localhost");
160+
IPHostEntry host = Dns.Resolve("127.0.0.1");
161161
IPAddress ipAddress = host.AddressList[0];
162162
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, this.Settings.port);
163163
this.listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
164164
this.listener.Bind(localEndPoint);
165165
this.listener.Listen(100); //connection limit
166-
Console.WriteLine("Listening on http://localhost:{0}", this.Settings.port);
166+
Console.WriteLine("Listening on http://127.0.0.1:{0}", this.Settings.port);
167167
while (running)
168168
{
169-
Socket handler = this.listener.Accept();
170-
Thread t = new Thread(onRequest);
171-
t.Start(handler);
172-
//onRequest(handler);
169+
try
170+
{
171+
Socket handler = this.listener.Accept();
172+
Thread t = new Thread(onRequest);
173+
t.Start(handler);
174+
}
175+
catch (Exception e) {
176+
break;
177+
}
173178
}
174-
//handler.Shutdown(SocketShutdown.Both);
175-
//handler.Close();
179+
this.listener.Shutdown(SocketShutdown.Both);
180+
this.listener.Close();
176181
}
177182
catch (Exception e)
178183
{

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4
1+
2.6

0 commit comments

Comments
 (0)