Exemple #1
0
        static void Main(string[] args)
        {
            // defaults
            ushort FTSERVER_PORT  = 40000;
            int    CLIENT_BACKLOG = 5;
            string PRS_ADDRESS    = "127.0.0.1";
            ushort PRS_PORT       = 30000;
            string SERVICE_NAME   = "FT Server";

            // process the command line arguments to get the PRS ip address and PRS port number
            try
            {
                if (args.Length > 0)
                {
                    if (args[0] == "-prs")
                    {
                        string[] IP_PORT = args[1].Split(':');

                        PRS_ADDRESS = IP_PORT[0];
                        PRS_PORT    = ushort.Parse(IP_PORT[1]);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            Console.WriteLine("PRS Address: " + PRS_ADDRESS);
            Console.WriteLine("PRS Port: " + PRS_PORT);

            try
            {
                // contact the PRS, request a port for "FT Server" and start keeping it alive
                PRSClient prs = new PRSClient(PRS_ADDRESS, PRS_PORT, SERVICE_NAME);
                FTSERVER_PORT = prs.RequestPort();
                prs.KeepPortAlive();

                // instantiate FT server and start it running
                FTServer ft = new FTServer(FTSERVER_PORT, CLIENT_BACKLOG);
                ft.Start();

                // tell the PRS that it can have it's port back, we don't need it anymore
                prs.ClosePort();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            // wait for a keypress from the user before closing the console window
            Console.WriteLine("Press Enter to exit");
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            // TODO: SDServerProgram.Main()

            // defaults
            ushort SDSERVER_PORT  = 40000;
            int    CLIENT_BACKLOG = 5;
            string PRS_ADDRESS    = "127.0.0.1";
            ushort PRS_PORT       = 30000;
            string SERVICE_NAME   = "SD Server";

            // process the command line arguments to get the PRS ip address and PRS port number


            Console.WriteLine("PRS Address: " + PRS_ADDRESS);
            Console.WriteLine("PRS Port: " + PRS_PORT);

            try
            {
                // contact the PRS, request a port for "FT Server" and start keeping it alive
                PRSClient prs = new PRSClient(PRS_ADDRESS, PRS_PORT, SERVICE_NAME);
                SDSERVER_PORT = prs.RequestPort();
                prs.KeepPortAlive();

                // instantiate SD server and start it running
                SDServer sd = new SDServer(SDSERVER_PORT, CLIENT_BACKLOG);
                sd.Start();

                // tell the PRS that it can have it's port back, we don't need it anymore
                prs.ClosePort();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            // wait for a keypress from the user before closing the console window
            Console.WriteLine("Press Enter to exit");
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            // process cmd line
            // -prs <PRS IP address>:<PRS port>
            try
            {
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-prs")// -prs prsip:prsport
                    {
                        if (i++ < args.Length)
                        {
                            string[] parts = args[i].Split(':');
                            if (parts.Length != 2)
                            {
                                throw new Exception("Invalid PRSIP:PRSAddress format");
                            }
                            PRS_ADDRESS = parts[1];
                            PRS_PORT    = System.Convert.ToUInt16(parts[0]);
                        }
                        else
                        {
                            throw new Exception("No value for -prs option");
                        }
                    }
                    else
                    {
                        throw new Exception("Invalid cmdline parameter");
                    }
                }
            }catch (Exception ex)
            {
                Console.WriteLine("Error processing command line:" + ex);
                return;
            }
            // create the session table
            SessionTable sessionTable = new SessionTable();

            Console.WriteLine("PRS ADDRESS:" + PRS_ADDRESS);
            Console.WriteLine("PRS PORT:" + PRS_PORT);
            // get the listening port from the PRS for the "SD Server" service
            PRSClient PRS           = new PRSClient(IPAddress.Parse(PRS_ADDRESS), PRS_PORT, SERVICE_NAME);
            ushort    listeningPort = PRS.RequestPort();

            // create the TCP listening socket
            Socket listeningSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);

            listeningSocket.Bind(new IPEndPoint(IPAddress.Any, listeningPort));
            listeningSocket.Listen(CLIENT_BACKLOG);     // 42 is the number of clients that can be waiting for us to accept their connection
            Console.WriteLine("Listening for clients on port " + listeningPort.ToString());

            bool done = false;

            while (!done)
            {
                // wait for a client to connect
                Console.WriteLine("Ready to accept new client");
                Socket clientSocket = listeningSocket.Accept();
                Console.WriteLine("Accepted connection from client");

                // create a thread for this client, and then return to listening for more clients
                Console.WriteLine("Launch new thread for connected client");
                ClientThread clientThread = new ClientThread(clientSocket, sessionTable);
                clientThread.Start();
            }

            // close down the listening socket
            Console.WriteLine("Closing listening socket");
            listeningSocket.Close();

            // close the listening port that I received from the PRS
            PRS.ClosePort();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            // defaults
            ushort SDSERVER_PORT  = 40000;
            int    CLIENT_BACKLOG = 5;
            string PRS_ADDRESS    = "127.0.0.1";
            ushort PRS_PORT       = 30000;
            string SERVICE_NAME   = "SD Server";

            for (int i = 0; i < args.Length; i++)
            {
                var arg = args[i];
                switch (arg)
                {
                case "-prs":
                {
                    var parameters = args[++i].Split(':');
                    var ipAddress  = parameters[0];
                    var port       = parameters[1];

                    PRS_ADDRESS = ipAddress;
                    PRS_PORT    = ushort.Parse(port);
                }
                break;

                default:
                {
                    Console.WriteLine($"Invalid argument: {arg}");
                    Usage();
                }
                break;
                }
            }

            Console.WriteLine("PRS Address: " + PRS_ADDRESS);
            Console.WriteLine("PRS Port: " + PRS_PORT);

            try
            {
                // contact the PRS, request a port for "FT Server" and start keeping it alive
                var prs = new PRSClient(PRS_ADDRESS, PRS_PORT, SERVICE_NAME);
                SDSERVER_PORT = prs.RequestPort();
                prs.KeepPortAlive();

                // instantiate SD server and start it running
                var server = new SDServer(SDSERVER_PORT, CLIENT_BACKLOG);
                server.Start();

                // tell the PRS that it can have it's port back, we don't need it anymore
                prs.ClosePort();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            // wait for a keypress from the user before closing the console window
            Console.WriteLine("Press Enter to exit");
            Console.ReadKey();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            // defaults
            ushort FTSERVER_PORT  = 40000;
            int    CLIENT_BACKLOG = 5;
            string PRS_ADDRESS    = "127.0.0.1";
            ushort PRS_PORT       = 30000;
            string SERVICE_NAME   = "FT Server";

            // process the command line arguments to get the PRS ip address and PRS port number
            for (var i = 0; i < args.Length; i++)
            {
                var arg = args[i];
                switch (arg)
                {
                case "-prs":
                {
                    var prs = args[++i].Split(':');
                    PRS_ADDRESS = prs[0];
                    PRS_PORT    = ushort.Parse(prs[1]);
                }
                break;

                default:
                {
                    Console.WriteLine($"Invalid argument: {arg}");
                    Usage();
                    return;
                }
                }
            }
            Console.WriteLine("PRS Address: " + PRS_ADDRESS);
            Console.WriteLine("PRS Port: " + PRS_PORT);

            try
            {
                // contact the PRS, request a port for "FT Server" and start keeping it alive
                var prsClient = new PRSClient(PRS_ADDRESS, PRS_PORT, SERVICE_NAME);
                FTSERVER_PORT = prsClient.RequestPort();
                prsClient.KeepPortAlive();
                Console.WriteLine($"Received port {FTSERVER_PORT} from PRS Server");

                // instantiate FT server and start it running
                Console.WriteLine("Starting FT server");
                var ftServer = new FTServer(FTSERVER_PORT, CLIENT_BACKLOG);
                ftServer.Start();
                Console.WriteLine($"FT server stopped");

                // tell the PRS that it can have it's port back, we don't need it anymore
                prsClient.ClosePort();
                Console.WriteLine("FT server port closed");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            // wait for a keypress from the user before closing the console window
            Console.WriteLine("Press Enter to exit");
            Console.ReadKey();
        }