Exemplo n.º 1
0
        public void RunServer(Socket connection, bool p_debugMode, MTLogging p_log, Database p_database) //Basic "main" method for the class
        {
            debugMode = p_debugMode;                                                                     //Setting the debug mode to the one passed in
            log       = p_log;                                                                           //Setting the logging object to the one passed in
            database  = p_database;                                                                      //Database object set to the database object from the main
            locations = database.ReadDatabase();                                                         //Database set to the database in the database object
            hostName  = ((IPEndPoint)connection.RemoteEndPoint).Address.ToString();                      //Sets the hostname to the IPAddress

            NetworkStream socketStream = new NetworkStream(connection);                                  //Setting the network stream on the socket

            if (debugMode)
            {
                Console.WriteLine("Connection Established.\n"); //~~~~~DEBUGGING~~~~~
            }
            DoRequest(socketStream, connection);                //Does the request received from client
        }
Exemplo n.º 2
0
        public Interface(bool d, MTLogging lo, Database da)
        {
            InitializeComponent();
            debug = d;
            log   = lo;
            data  = da;

            debug = true;

            log = new MTLogging(t_logfile.Text);
            Int32.TryParse(t_timeout.Text, out timeout);
            data = new Database(t_database.Text);
            if (t_database.Text != null)
            {
                data.LoadDatabase();
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            TcpListener listener;
            Socket      connection;
            Server      server;

            int    timeout      = 1000;
            string fileName     = null;
            string databaseName = null;

            if (args.Length == 1)
            {
                if (args[0] == "-d")
                {
                    debugMode = true;
                    Console.WriteLine("Debug mode enabled.");
                }
                else if (args[0] == "-w")
                {
                    Application ui = new Application();
                    ui.Run(new Interface(debugMode, log, database));
                }
                else
                {
                    Console.WriteLine("Arguments invalid.");
                }
            } //~~~~~DEBUGGING~~~~~
            else if (args.Length > 1)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-d")
                    {
                        debugMode = true;
                        Console.WriteLine("Debug Mode Activated.");
                    }
                    else if (args[i] == "-t")
                    {
                        try
                        {
                            timeout = Int32.Parse(args[i + 1]);
                            if (debugMode && timeout != 0)
                            {
                                Console.WriteLine("Timeout time set to: " + timeout.ToString() + "ms");
                            }
                            else if (debugMode && timeout == 0)
                            {
                                Console.WriteLine("Timeout time deactivated.");
                            }
                        }
                        catch
                        {
                            if (debugMode)
                            {
                                Console.WriteLine("Invalid arguments given. Timeout set to default (1000ms).");
                            }
                        }
                    }
                    else if (args[i] == "-l")
                    {
                        try
                        {
                            fileName = args[i + 1];
                            if (debugMode)
                            {
                                Console.WriteLine("Log filename set to: " + fileName);
                            }
                        }
                        catch
                        {
                            if (debugMode)
                            {
                                Console.WriteLine("Invalid file name.");
                            }
                        }
                    }
                    else if (args[i] == "-f")
                    {
                        try
                        {
                            databaseName = args[i + 1];
                            if (debugMode)
                            {
                                Console.WriteLine("Database filename set to: " + databaseName);
                            }
                        }
                        catch
                        {
                            if (debugMode)
                            {
                                Console.WriteLine("Invalid file name.");
                            }
                        }
                    }
                    else if (args[i] == "-w")
                    {
                        Application ui = new Application();
                        ui.Run(new Interface(debugMode, log, database));
                    }
                }
            } //~~~~~DEBUGGING~~~~~
            else if (args.Length == 0)
            {
                if (debugMode)
                {
                    Console.WriteLine("Everything set to default.");
                }
            }

            if (debugMode)
            {
                Console.WriteLine("\nServer running...\n~~~~~~~~~~~~~~~~~~~~~~~~~");           //~~~~~DEBUGGING~~~~~
            }
            log      = new MTLogging(fileName);
            database = new Database(databaseName);
            if (databaseName != null)
            {
                database.LoadDatabase();
            }

            try
            {
                listener = new TcpListener(IPAddress.Any, 43); //Listens for data packets on port 43.
                listener.Start();                              //Starts the listener.

                while (true)                                   //While it is connected.
                {
                    connection = listener.AcceptSocket();      //TCPListner connects a socket
                    if (timeout != 0)
                    {
                        connection.ReceiveTimeout = timeout; //Set timeout
                        connection.SendTimeout    = timeout; //Set timeout
                    }

                    server = new Server();                                                               //Creates a new object of server
                    Thread t = new Thread(() => server.RunServer(connection, debugMode, log, database)); //Creates a new thread running the server object with it's method
                    t.Start();                                                                           //Starts the thread
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.ToString());
            }
        }