Example #1
0
        static void Main(string[] args)
        {
            HttpServer server = new HttpServer(5000);

            server.Start();
        }
Example #2
0
        static int Main(string[] args)
        {
            string   path;
            int      port;
            DateTime timeA = DateTime.Now;
            DateTime timeB;

            if (args.GetLength(0) > 0)
            {
                if (args[0] == "--path")
                {
                    path = args[1];
                    port = 80;

                    if (!Directory.Exists(path))
                    {
                        Console.WriteLine("PATH inexistente: " + path);
                        return(1);
                    }
                }

                else if (args[0] == "--port")
                {
                    int  i;
                    bool b = Int32.TryParse(args[1], out i);

                    if (!b)
                    {
                        Console.WriteLine("Puerto no es un nĂºmero");
                        return(1);
                    }

                    port = Convert.ToInt16(args[1]);
                    path = System.Reflection.Assembly.GetEntryAssembly().Location;
                }

                else if (args[0] == "--path" && args[2] == "--port")
                {
                    int  i;
                    bool b = Int32.TryParse(args[2], out i);

                    if (!b)
                    {
                        Console.WriteLine("PORT is not a number");
                        return(1);
                    }

                    path = args[1];
                    port = Convert.ToInt16(args[3]);

                    if (!Directory.Exists(path))
                    {
                        Console.WriteLine("PATH does not exist: " + path);
                        return(1);
                    }
                }

                else if (args[2] == "--path" && args[0] == "--port")
                {
                    int  i;
                    bool b = Int32.TryParse(args[1], out i);

                    if (!b)
                    {
                        Console.WriteLine("PORT is not a number");
                        return(1);
                    }

                    path = args[3];
                    port = Convert.ToInt16(args[1]);

                    if (!Directory.Exists(path))
                    {
                        Console.WriteLine("PATH does not exist: " + path);
                        return(1);
                    }
                }
                else
                {
                    path = args[1];
                    port = Convert.ToInt16(args[3]);
                }
            }
            else
            {
                path = System.Reflection.Assembly.GetEntryAssembly().Location;
                port = 80;
            }
            Console.WriteLine("PATH: " + path);
            Console.WriteLine("PORT: " + port);
            Program o = new Program();

            o._checkport(port);
            HttpServer Server = new HttpServer(port);

            //Server.StateChanged += (sender, evt) =>
            //{
            //    Console.WriteLine(sender.GetType().Name);
            //    Console.WriteLine("Changed");
            //};

            Server.Start();

            bool bs = true;

            while (bs)
            {
                if (Console.ReadLine() == "uptime" || Console.ReadLine() == "UPTIME")
                {
                    timeB = DateTime.Now;
                    o._runTime(timeA, timeB);
                }
                if (Console.ReadLine() == "kill" || Console.ReadLine() == "KILL")
                {
                    bs = false;
                }
            }
            Server.Stop();
            return(0);
            //Console.ReadKey();
        }
Example #3
0
        static int Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("Initializing Server App... ");

            int    portNum;
            string port = null;
            string path = null;

            // Getting args
            if (args.Length > 1)
            {
                if (args[0] == "--port")
                {
                    port = args[1];
                }
                if (args[0] == "--path")
                {
                    path = args[1];
                }
            }

            if (args.Length > 3)
            {
                if (args[2] == "--port")
                {
                    port = args[3];
                }
                if (args[2] == "--path")
                {
                    path = args[3];
                }
            }

            // Validating Port
            if (!int.TryParse(port, out portNum))
            {
                if (port != null)
                {
                    Console.WriteLine("ERROR: Port parameter provided is not a integer number.");
                    return(1);
                }
                portNum = 8081;
            }

            // Validating Path
            if (path != null)
            {
                if (!Directory.Exists(path))
                {
                    Console.WriteLine("The path \"{0}\" does not exist.", path);
                    return(1);
                }
            }
            else
            {
                path = Directory.GetCurrentDirectory();
            }

            // Notifying App initialization
            Console.WriteLine("Server App initialization Complete!");
            Console.WriteLine($"Using PORT : {portNum}");
            Console.WriteLine($"Using PATH : {path}");
            Console.WriteLine();

            //--------------- Listen --------------//

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            //string cmd;

            Console.WriteLine($"Listening on port {portNum}...");

            var httpServer = new HttpServer(portNum);

            httpServer.StateChanged += (sender, e) =>
            {
                var st = e as StateChangedEventArgs;

                Console.Write("State Changed: ");
                Console.Write(st.PreviousState.ToString());
                Console.Write(" -> ");
                Console.Write(st.CurrentState.ToString());
                Console.WriteLine();
            };


            httpServer.Start();

            //bool breaker = true;

            //while (breaker)
            //{
            //    cmd = Console.ReadLine();
            //    if (cmd.ToLower() == "uptime")
            //    {
            //        Console.WriteLine("Time since Server started: " + stopWatch.Elapsed);
            //    }
            //    else if (cmd.ToLower() == "break")
            //    { breaker = false; }
            //};

            Console.ReadKey();

            httpServer.Stop();
            stopWatch.Stop();

            return(0);
        }