private void HandleConnection(object obj)
        {
            TcpClient client = obj as TcpClient;

            Stopwatch watch   = Stopwatch.StartNew();
            long      timeout = 1000 * 60 * 10; // 10 seconds

            using (NetworkStream stream = client.GetStream())
            {
                while (watch.ElapsedMilliseconds <= timeout && client.Connected)
                {
                    if (stream.DataAvailable)
                    {
                        HandleRequest(stream);

                        watch.Restart();
                    }

                    HandleAlerts(stream);
                }

                stream.Flush();

                client.Dispose();

                client.Close();

                //Register user logout
                User user = users[Thread.CurrentThread];
                users.Remove(Thread.CurrentThread);
                ServerProgram.RemoveFromLive(user);
            }
        }
Example #2
0
        //Implement Interface IClientServices
        public Object commandInterpreter(Command c)
        {
            if (Root == false)
            {
                return(false);
            }
            else
            {
                while (ServerProgram.checkSMRState() == 0)
                {
                    ;
                }
                switch (c.getCommand())
                {
                case "READ":
                    return(image.Read((int)c.getPayload()));

                case "ADD":
                    break;

                case "TAKE":
                    break;
                }
            }
            return(true);
        }
Example #3
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            ServerProgram server = new ServerProgram();

            server.SetTextFunction += Server_SetTextFunction;

            server.StartServer();
            btnStart.Enabled = false;
        }
Example #4
0
        static void Main(string[] args)
        {
            ServerProgram s = new ServerProgram(IPAddress.Loopback, 1024);

            s.Start();

            Console.ReadLine();
            s.Stop();
        }
        static void Main(string[] args)
        {
            #region setup
            //  Allocate a buffer to store incoming data
            byte[] IncomingBuffer = new byte[1024];
            var    app            = new ServerProgram();

            //  Establish a local endpoint for the socket
            IPHostEntry HostInfo      = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   IPAddress     = HostInfo.AddressList[0];
            IPEndPoint  LocalEndPoint = new IPEndPoint(IPAddress, 1200);

            //  Create the socket
            Socket listener = new Socket(IPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            // Log server ip
            var BorderLine = String.Concat(Enumerable.Repeat("-", Console.WindowWidth));
            Console.WriteLine($"Server IP : {LocalEndPoint}\n{BorderLine}");
            #endregion

            try
            {
                //  Bind the socket to the local endpoint
                listener.Bind(LocalEndPoint);

                //  Listen for incoming connections
                listener.Listen(1);

                //  Enter a loop
                while (true)
                {
                    //  Listen for a connection (the program is blocked)
                    Console.WriteLine("Waiting for connection...");
                    Socket handler = listener.Accept();
                    Console.WriteLine($"Connection accepted!");

                    HandleConnection(handler);
                } //while
            }     //try
            catch (Exception e)
            {
                Console.WriteLine($"EXCEPTION : {e}");
            } //catch
        }     //main
Example #6
0
        public Server()
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;

            serverProgram = new ServerProgram();

            serverProgram.SetClientPath(txtClientPath.Text);
            serverProgram.SetServerPath(txtServerPath.Text);

            serverProgram.OnServerStarted     += HandleOnServerStarted;
            serverProgram.OnClientListChanged += HandleOnClientListChanged;

            countdown          = new System.Timers.Timer();
            countdown.Elapsed += Countdown_Elapsed;
            countdown.Interval = 1000;

            serverProgram.Start();

            InitPopupNotifier();
        }
Example #7
0
        static void Main(string[] args)
        {
            var app = new ServerProgram();

            // Establish a local endpoint for the socket
            IPHostEntry ipHostInfo    = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ipAddress     = ipHostInfo.AddressList[0];
            IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, 11000);

            // Create the socket
            var listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                // Bind the socket to the local endpoint
                listener.Bind(localEndPoint);
                // Listen for incoming connections
                listener.Listen(10);

                // Loop
                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    //    Listen for a connection (blocking call)
                    Socket handler = listener.Accept();

                    Task handleRequest = Task.Factory.StartNew(
                        () => app.HandleRequest(handler)

                        );
                } // while(true)
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception : {0}", e.ToString());
            }
            Console.WriteLine("\nPress ENTER to exit...");
            Console.Read();
        }
Example #8
0
        public static Response UserDisconnectRequestHandler(UserDisconnectRequest arg)
        {
            ServerProgram.RemoveFromLive(arg.userDisconnected);

            return(Response.Ok());
        }