Last active
December 22, 2015 08:08
-
-
Save rudym/6442313 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Look for lines 71 and 112! | |
//Try to connect from your browser to this server | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Threading; | |
namespace httpserver | |
{ | |
public class CustHttpServer | |
{ | |
#region Fields | |
private const int Clientlimit = 50; | |
private int _port; | |
private bool _isServerRunning = false; | |
private TcpListener _listener; | |
#endregion | |
#region Constructor | |
public CustHttpServer(int port) | |
{ | |
if (port < 0 || port > 65535) | |
{ | |
Console.WriteLine("ERROR: PORT NUMBER OUT OF RANGE"); | |
return; | |
} | |
this._port = port; | |
} | |
#endregion | |
public void Start() | |
{ | |
try | |
{ | |
if (_isServerRunning) return; | |
Run(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.ToString()); | |
} | |
} | |
public void Stop() | |
{ | |
if (_isServerRunning) | |
{ | |
_isServerRunning = false; | |
Console.WriteLine("Server Stopped"); | |
} | |
} | |
private void Run() | |
{ | |
try | |
{ | |
_isServerRunning = true; | |
_listener = new TcpListener(IPAddress.Parse(LocalIpAddress()), _port); | |
_listener.Start(); | |
for (int i = 0; i < Clientlimit; i++) | |
{ | |
//For now opening thread for each connection (total # is Clientlimit), must change to async | |
var t = new Thread(new ThreadStart(Service)) {IsBackground = true}; | |
t.Start(); | |
} | |
Console.WriteLine(String.Format("Server Start (Port Number: {0})", _port)); | |
} | |
catch (SocketException ex) | |
{ | |
Console.WriteLine(String.Format("ERROR: Server Start Failure (Port Number: {0}) : {1}", _port, | |
ex.Message)); | |
Console.WriteLine(ex.ToString()); | |
_isServerRunning = false; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.ToString()); | |
_isServerRunning = true; | |
} | |
} | |
private void Service() | |
{ | |
try | |
{ | |
if (_isServerRunning) | |
Console.WriteLine(String.Format("Waiting for clients on: {0}:{1}", LocalIpAddress(), _port)); | |
while (_isServerRunning) | |
{ | |
Socket s = _listener.AcceptSocket(); | |
Console.WriteLine(String.Format("Client Connected : {0}", s.RemoteEndPoint)); | |
try | |
{ | |
Stream stream = new NetworkStream(s); | |
StreamReader sr = new StreamReader(stream); | |
StreamWriter sw = new StreamWriter(stream); | |
sw.AutoFlush = true; | |
//mb change to async write? | |
sw.Write("Hello World!"); | |
string serial = sr.ReadLine(); | |
Console.WriteLine(serial); | |
stream.Close(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(String.Format("ERROR: Socket Error: {0}, {1}", s.RemoteEndPoint, ex.Message)); | |
Console.WriteLine(ex.ToString()); | |
} | |
Console.WriteLine(String.Format("End Connection : {0}", s.RemoteEndPoint)); | |
s.Close(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.ToString()); | |
} | |
} | |
private static string LocalIpAddress() | |
{ | |
string localIp = String.Empty; | |
try | |
{ | |
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); | |
foreach (IPAddress ip in host.AddressList.Where(ip => ip.AddressFamily.ToString() == "InterNetwork").Take(1)) | |
{ | |
localIp = ip.ToString(); | |
} | |
return localIp; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.ToString()); | |
} | |
return localIp; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment