예제 #1
0
        public string GetDocument(string serverIP, string documentName)
        {
            // retrieve requested document from the specified server
            // manage the session with the SD Server
            //  opening or resuming as needed
            // connect to and disconnect from the server w/in this method

            // make sure we have valid parameters
            // serverIP is the SD Server's IP address
            // documentName is the name of a docoument on the SD Server
            // both should not be empty
            if (String.IsNullOrWhiteSpace(serverIP) || String.IsNullOrWhiteSpace(documentName))
            {
                throw new Exception("EMpty server IP or document name!");
            }

            // contact the PRS and lookup port for "SD Server"
            PRSClient prs    = new PRSClient(prsIP, prsPort, "SD Server");
            ushort    sdPort = prs.LookupPort();

            // connect to SD server by ipAddr and port
            // use OpenOrResumeSession() to ensure session is handled correctly
            SDClient client = OpenOrResumeSession(serverIP, sdPort);

            // send get message to server for requested document
            string content = client.GetDocument(documentName);

            // disconnect from server
            client.Disconnect();

            // return the content
            return(content);
        }
예제 #2
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();
        }
예제 #3
0
            public void ConnecttoServer()
            {
                // get the server port from the PRS for the "FT Server" service
                PRSClient prs        = new PRSClient(IPAddress.Parse(prsip), PRS_PORT, "FT Server");
                ushort    serverPort = prs.LookupPort();

                // connect to the server on it's IP address and port
                //Console.WriteLine("Connecting to server at " + serverIP + ":" + serverPort.ToString());
                Socket sock = new Socket(SocketType.Stream, ProtocolType.Tcp);

                sock.Connect(IPAddress.Parse(ServerIP), serverPort);
                //Console.WriteLine("Connected to server");

                // establish network stream and reader/writers for the socket
                socketNetworkStream = new NetworkStream(sock);
                socketReader        = new StreamReader(socketNetworkStream);
                socketWriter        = new StreamWriter(socketNetworkStream);
            }
        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();
        }
        public string GetDocument(string serverIP, string documentName)
        {
            // make sure we have valid parameters
            // serverIP is the FT Server's IP address
            // documentName is the name of a directory on the FT Server
            // both should not be empty
            if (String.IsNullOrWhiteSpace(serverIP) || String.IsNullOrWhiteSpace(documentName))
            {
                throw new Exception("Empty server IP or document Name!");
            }

            // contact the PRS and lookup port for "FT Server"
            PRSClient prs    = new PRSClient(prsIP, prsPort, "FT Server");
            ushort    ftPort = prs.LookupPort();

            // connect to FT server by ipAddr and port
            FTClient ft = new FTClient(serverIP, ftPort);

            ft.Connect();

            // get the files from the requested directory
            FTClient.FileContent[] files = ft.GetDirectory(documentName);

            // accumulate file contents in a result string
            StringBuilder builder = new StringBuilder();

            foreach (FTClient.FileContent file in files)
            {
                // name, content and empty line for each file
                builder.AppendLine(file.Name);
                builder.AppendLine(file.Content);
                builder.AppendLine();
            }

            // disconnect from server
            ft.Disconnect();

            // return the content
            return(builder.ToString());
        }
예제 #6
0
        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();
        }
예제 #7
0
        static void Main(string[] args)
        {
            // defaults
            string PRSSERVER_IPADDRESS = "127.0.0.1";
            ushort PRSSERVER_PORT      = 30000;
            string SDSERVICE_NAME      = "SD Server";
            string SDSERVER_IPADDRESS  = "127.0.0.1";
            ushort SDSERVER_PORT       = 40000;
            string SESSION_CMD         = null;
            ulong  SESSION_ID          = 0;
            string DOCUMENT_CMD        = null;
            string DOCUMENT_NAME       = null;

            // process the command line arguments
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] == "-o")
                {
                    SESSION_CMD = "-o";
                }
                else if (args[i] == "-c")
                {
                    SESSION_CMD = "-c";
                    SESSION_ID  = ulong.Parse(args[++i]);
                }
                else if (args[i] == "-r")
                {
                    SESSION_CMD = "-r";
                    SESSION_ID  = ulong.Parse(args[++i]);
                }
                else if (args[i] == "-post" || args[i] == "-get")
                {
                    DOCUMENT_CMD  = args[i];
                    DOCUMENT_NAME = args[++i];
                }
            }


            Console.WriteLine("PRS Address: " + PRSSERVER_IPADDRESS);
            Console.WriteLine("PRS Port: " + PRSSERVER_PORT);
            Console.WriteLine("SD Server Address: " + SDSERVER_IPADDRESS);
            Console.WriteLine("Session Command: " + SESSION_CMD);
            Console.WriteLine("Session Id: " + SESSION_ID);
            Console.WriteLine("Document Command: " + DOCUMENT_CMD);
            Console.WriteLine("Document Name: " + DOCUMENT_NAME);

            try
            {
                // contact the PRS and lookup port for "SD Server"
                PRSClient prs = new PRSClient(PRSSERVER_IPADDRESS, PRSSERVER_PORT, SDSERVICE_NAME);
                SDSERVER_PORT = prs.LookupPort();

                // create an SDClient to use in talking to the server
                SDClientLib.SDClient sd = new SDClientLib.SDClient(SDSERVER_IPADDRESS, SDSERVER_PORT);
                sd.Connect();

                // send session command to server
                if (SESSION_CMD == "-o")
                {
                    // open new session
                    sd.OpenSession();
                }
                else if (SESSION_CMD == "-r")
                {
                    // resume existing session
                    sd.ResumeSession(SESSION_ID);
                }
                else if (SESSION_CMD == "-c")
                {
                    // close existing session
                    sd.SessionID = SESSION_ID;
                    sd.CloseSession();
                }

                // send document request to server
                if (DOCUMENT_CMD == "-post")
                {
                    // read the document contents from stdin
                    string documentContents = Console.In.ReadToEnd();

                    // send the document to the server
                    sd.PostDocument(DOCUMENT_NAME, documentContents);
                }
                else if (DOCUMENT_CMD == "-get")
                {
                    // get document from the server
                    string documentContents = sd.GetDocument(DOCUMENT_NAME);

                    // print out the received document
                    Console.WriteLine("Received document content: " + documentContents);
                }

                // disconnect from the server
                sd.Disconnect();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            // wait for a keypress from the user before closing the console window
            // NOTE: the following commented out as they cannot be used when redirecting input to post a file
            //Console.WriteLine("Press Enter to exit");
            //Console.ReadKey();
        }
예제 #8
0
        static void Main(string[] args)
        {
            // defaults
            string PRSSERVER_IPADDRESS = "127.0.0.1";
            ushort PRSSERVER_PORT      = 30000;
            string SDSERVICE_NAME      = "SD Server";
            string SDSERVER_IPADDRESS  = "127.0.0.1";
            ushort SDSERVER_PORT       = 40000;
            string SESSION_CMD         = null;
            ulong  SESSION_ID          = 0;
            string DOCUMENT_CMD        = null;
            string DOCUMENT_NAME       = null;

            // process the command line arguments
            for (var i = 0; i < args.Length; i++)
            {
                var arg = args[i];
                switch (arg)
                {
                case "-r":
                case "-c":
                {
                    SESSION_CMD = arg;
                    SESSION_ID  = ulong.Parse(args[++i]);
                }
                break;

                case "-o":
                {
                    SESSION_CMD = arg;
                }
                break;

                case "-get":
                case "-post":
                {
                    DOCUMENT_CMD  = arg;
                    DOCUMENT_NAME = args[++i];
                }
                break;

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

            Console.WriteLine("PRS Address: " + PRSSERVER_IPADDRESS);
            Console.WriteLine("PRS Port: " + PRSSERVER_PORT);
            Console.WriteLine("SD Server Address: " + SDSERVER_IPADDRESS);
            Console.WriteLine("Session Command: " + SESSION_CMD);
            Console.WriteLine("Session Id: " + SESSION_ID);
            Console.WriteLine("Document Command: " + DOCUMENT_CMD);
            Console.WriteLine("Document Name: " + DOCUMENT_NAME);

            try
            {
                // contact the PRS and lookup port for "SD Server"
                var prs = new PRSClient(PRSSERVER_IPADDRESS, PRSSERVER_PORT, SDSERVICE_NAME);

                // create an SDClient to use in talking to the server
                var client = new SDClient(SDSERVER_IPADDRESS, SDSERVER_PORT);
                client.Connect();

                // send session command to server
                if (SESSION_CMD == "-o")
                {
                    // open new session
                    client.OpenSession();
                    Console.WriteLine($"Opened session {client.SessionID}");
                }
                else if (SESSION_CMD == "-r")
                {
                    // resume existing session
                    Console.WriteLine($"Resuming session id {SESSION_ID}");
                    client.SessionID = SESSION_ID;
                    client.ResumeSession();
                    Console.WriteLine($"Accepted session id {SESSION_ID}");
                }
                else if (SESSION_CMD == "-c")
                {
                    Console.WriteLine($"Closing session id {SESSION_ID}");
                    // close existing session
                    client.SessionID = SESSION_ID;
                    client.CloseSession();
                    Console.WriteLine($"Closed session id {SESSION_ID}");
                }

                // send document request to server
                if (DOCUMENT_CMD == "-post")
                {
                    // read the document contents from stdin
                    var contents = Console.In.ReadToEnd();

                    // send the document to the server
                    Console.WriteLine($"Posting {contents.Length} bytes of {DOCUMENT_NAME}");
                    client.PostDocument(DOCUMENT_NAME, contents);

                    Console.WriteLine("Success");
                }
                else if (DOCUMENT_CMD == "-get")
                {
                    Console.WriteLine($"Getting {DOCUMENT_NAME}");
                    // get document from the server
                    var contents = client.GetDocument(DOCUMENT_NAME);

                    // print out the received document
                    Console.WriteLine($"Success, received {contents.Length} bytes of {DOCUMENT_NAME}");
                    Console.WriteLine(contents);
                }

                // disconnect from the server
                client.Disconnect();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            // wait for a keypress from the user before closing the console window
            // NOTE: the following commented out as they cannot be used when redirecting input to post a file
            //Console.WriteLine("Press Enter to exit");
            //Console.ReadKey();
        }
예제 #9
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();
        }
예제 #10
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();
        }
예제 #11
0
        static void Main(string[] args)
        {
            // defaults
            string PRSSERVER_IPADDRESS = "127.0.0.1";
            ushort PSRSERVER_PORT      = 30000;
            string FTSERVICE_NAME      = "FT Server";
            string FTSERVER_IPADDRESS  = "127.0.0.1";
            ushort FTSERVER_PORT       = 40000;
            string DIRECTORY_NAME      = "foo";

            // process the command line arguments

            try
            {
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-d")
                    {
                        DIRECTORY_NAME = args[++i];
                    }
                }

                if (DIRECTORY_NAME == null)
                {
                    throw new Exception("FTClientProgram: Directory name is null");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("FTClientProgram.Main() - Error: " + ex.Message);
                Usage();
                return;
            }


            Console.WriteLine("PRS Address: " + PRSSERVER_IPADDRESS);
            Console.WriteLine("PRS Port: " + PSRSERVER_PORT);
            Console.WriteLine("FT Server Address: " + FTSERVER_IPADDRESS);
            Console.WriteLine("Directory: " + DIRECTORY_NAME);

            try
            {
                // contact the PRS and lookup port for "FT Server"
                PRSClient prs = new PRSClient(PRSSERVER_IPADDRESS, PSRSERVER_PORT, FTSERVICE_NAME);
                FTSERVER_PORT = prs.LookupPort();

                // create an FTClient and connect it to the server
                FTClientLib.FTClient ft = new FTClientLib.FTClient(FTSERVER_IPADDRESS, FTSERVER_PORT);
                ft.Connect();

                // get the contents of the specified directory
                FTClientLib.FTClient.FileContent[] files = ft.GetDirectory(DIRECTORY_NAME);

                // Create the local directory if needed
                Directory.CreateDirectory(DIRECTORY_NAME);

                // Save the files locally on the disk
                foreach (FTClientLib.FTClient.FileContent file in files)
                {
                    File.WriteAllText(Path.Combine(DIRECTORY_NAME, file.Name), file.Content);
                }

                // disconnect from the server
                ft.Disconnect();
            }
            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();
        }
예제 #12
0
        static void Main(string[] args)
        {
            // defaults
            string PRSSERVER_IPADDRESS = "127.0.0.1";
            ushort PRSSERVER_PORT      = 30000;
            string FTSERVICE_NAME      = "FT Server";
            string FTSERVER_IPADDRESS  = "127.0.0.1";
            ushort FTSERVER_PORT       = 40000;
            string DIRECTORY_NAME      = null;

            // process the command line arguments

            try
            {
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-d")
                    {
                        DIRECTORY_NAME = args[++i];
                    }
                    if (args[i] == "-prs")
                    {
                        string[] IP_PORT = args[1].Split(':');

                        PRSSERVER_IPADDRESS = IP_PORT[0];
                        PRSSERVER_PORT      = ushort.Parse(IP_PORT[1]);
                    }
                    if (args[i] == "-s")
                    {
                        FTSERVER_IPADDRESS = args[++i];
                    }
                }

                if (DIRECTORY_NAME == null)
                {
                    throw new Exception("Missing required directory name!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            Console.WriteLine("PRS Address: " + PRSSERVER_IPADDRESS);
            Console.WriteLine("PRS Port: " + PRSSERVER_PORT);
            Console.WriteLine("FT Server Address: " + FTSERVER_IPADDRESS);
            Console.WriteLine("Directory: " + DIRECTORY_NAME);

            try
            {
                // contact the PRS and lookup port for "FT Server"
                PRSClient prs = new PRSClient(PRSSERVER_IPADDRESS, PRSSERVER_PORT, FTSERVICE_NAME);
                FTSERVER_PORT = prs.LookupPort();

                // create an FTClient and connect it to the server
                FTClient ft = new FTClient(FTSERVER_IPADDRESS, FTSERVER_PORT);
                ft.Connect();

                // get the contents of the specified directory
                ft.GetDirectory(DIRECTORY_NAME);

                // disconnect from the server
                ft.Disconnect();
            }
            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();
        }
예제 #13
0
        static void Main(string[] args)
        {
            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 if (args[i] == "-get")//-get documentname
                    {
                        if (i++ < args.Length)
                        {
                            DOCUMENT_NAME = args[i];
                            GET           = true;
                        }
                        else
                        {
                            throw new Exception("No value for -get option");
                        }
                    }
                    else if (args[i] == "-post")//-post documentname
                    {
                        if (i++ < args.Length)
                        {
                            DOCUMENT_NAME = args[i];
                            POST          = true;
                        }
                        else
                        {
                            throw new Exception("No value for -post option");
                        }
                    }
                    else if (args[i] == "-o")//-o
                    {
                        OPEN_SESSION = true;
                    }
                    else if (args[i] == "-r")//-r sessionid
                    {
                        if (i++ < args.Length)
                        {
                            SESSION_ID     = System.Convert.ToUInt64(args[i]);
                            RESUME_SESSION = true;
                        }
                        else
                        {
                            throw new Exception("No value for -r option");
                        }
                    }
                    else if (args[i] == "-c")//-c sessionid | -r sessionid -c | -o sessionid -c
                    {
                        if (i++ < args.Length)
                        {
                            SESSION_ID    = System.Convert.ToUInt64(args[i]);
                            CLOSE_SESSION = true;
                        }
                        else
                        {
                            throw new Exception("No value for -o option");
                        }
                    }
                    else if (args[i] == "-s")//-s serveraddress
                    {
                        if (i++ < args.Length)
                        {
                            SERVER_ADDRESS = args[i];
                        }
                        else
                        {
                            throw new Exception("No value for -s option");
                        }
                    }
                    else
                    {
                        throw new Exception("Invalid cmdline parameter");
                    }
                }
            }catch (Exception ex)
            {
                Console.WriteLine("Error processing command line:" + ex);
                return;
            }
            Console.WriteLine("PRS ADDRESS:" + PRS_ADDRESS);
            Console.WriteLine("PRS PORT:" + PRS_PORT);
            Console.WriteLine("SERVER ADDRESS" + SERVER_ADDRESS);
            Console.WriteLine("OPEN SESSION:" + OPEN_SESSION.ToString());
            Console.WriteLine("CLOSE SESSION:" + CLOSE_SESSION.ToString());
            Console.WriteLine("RESUME SESSION:" + RESUME_SESSION.ToString());
            Console.WriteLine("RESUME SESSION ID:" + SESSION_ID.ToString());
            Console.WriteLine("GET:" + GET.ToString());
            Console.WriteLine("POST:" + POST.ToString());
            Console.WriteLine("DOCUMENTNAME" + DOCUMENT_NAME);
            Console.WriteLine();



            // Get port from the PRS
            PRSClient prs        = new PRSClient(IPAddress.Parse(PRS_ADDRESS), PRS_PORT, SERVICE_NAME);
            ushort    serverPort = prs.LookupPort();

            // connect to the server on it's IP address and port
            Console.WriteLine("Connecting to server at " + SERVER_ADDRESS + ":" + serverPort.ToString());
            Socket sock = new Socket(SocketType.Stream, ProtocolType.Tcp);

            sock.Connect(IPAddress.Parse(SERVER_ADDRESS), serverPort);
            Console.WriteLine("Connected to server");
            //establish network stream
            NetworkStream SocketNetworkStream = new NetworkStream(sock);
            StreamReader  socketReader        = new StreamReader(SocketNetworkStream);
            StreamWriter  socketWriter        = new StreamWriter(SocketNetworkStream);



            //order of commands is o or r then get or post then close
            if (OPEN_SESSION)
            {
                SESSION_ID = OpenSession(socketReader, socketWriter);
            }
            else if (RESUME_SESSION)
            {
                ResumeSession(socketReader, socketWriter);
            }

            //get/post block
            if (GET)
            {
                Get(socketReader, socketWriter);
            }
            else if (POST)
            {
                Post(socketReader, socketWriter);
            }
            //close block
            if (CLOSE_SESSION)
            {
                CloseSession(socketReader, socketWriter);
            }



            //socketWriter.WriteLine("get");
            //socketWriter.WriteLine(directoryName);
            //socketWriter.Flush();
            //Console.WriteLine("Sent get " + directoryName);

            //bool done = false;
            //while (!done)
            //{
            //    string cmdstring = socketReader.ReadLine();
            //    if (cmdstring == "done")
            //        done = true;
            //    else
            //    {
            //        string filename = cmdstring;
            //        string lengthstring = socketReader.ReadLine();
            //        int filelength = System.Convert.ToInt32(lengthstring);
            //        char[] buffer = new char[filelength];
            //        socketReader.Read(buffer, 0, filelength);
            //        string filecontents = new string(buffer);
            //        File.WriteAllText(Path.Combine(directoryName, filename), filecontents);
            //    }

            //}

            // disconnect from the server and close socket
            Console.WriteLine("Disconnecting from server");
            sock.Disconnect(false);
            socketReader.Close();
            socketWriter.Close();
            SocketNetworkStream.Close();
            sock.Close();
            Console.WriteLine("Disconnected from server");
        }
예제 #14
0
        static void Main(string[] args)
        {
            // defaults
            string PRSSERVER_IPADDRESS = "127.0.0.1";
            ushort PRSSERVER_PORT      = 30000;
            string FTSERVICE_NAME      = "FT Server";
            string FTSERVER_IPADDRESS  = "127.0.0.1";
            ushort FTSERVER_PORT       = 40000;
            string DIRECTORY_NAME      = null;

            // process the command line arguments
            for (var i = 0; i < args.Length; i++)
            {
                var arg = args[i];
                switch (arg)
                {
                case "-d":
                {
                    DIRECTORY_NAME = args[++i];
                }
                break;

                case "-prs":
                {
                    var prs = args[++i];
                    PRSSERVER_IPADDRESS = prs.Split(':')[0];
                    PRSSERVER_PORT      = ushort.Parse(prs.Split(':')[1]);
                }
                break;

                case "-s":
                {
                    FTSERVER_IPADDRESS = args[++i];
                }
                break;

                default:
                {
                    Console.WriteLine($"Invalid argument: {arg}");
                    Usage();
                    return;
                }
                }
            }
            Console.WriteLine("PRS Address: " + PRSSERVER_IPADDRESS);
            Console.WriteLine("PRS Port: " + PRSSERVER_PORT);
            Console.WriteLine("FT Server Address: " + FTSERVER_IPADDRESS);
            Console.WriteLine("Directory: " + DIRECTORY_NAME);

            try
            {
                // contact the PRS and lookup port for "FT Server"
                var prs = new PRSClient(PRSSERVER_IPADDRESS, PRSSERVER_PORT, FTSERVICE_NAME);
                FTSERVER_PORT = prs.LookupPort();
                Console.WriteLine($"Received port {FTSERVER_PORT} from PRS server");

                // create an FTClient and connect it to the server
                var ftClient = new FTClient(FTSERVER_IPADDRESS, FTSERVER_PORT);
                ftClient.Connect();
                Console.WriteLine("FT client connected");

                // get the contents of the specified directory
                ftClient.GetDirectory(DIRECTORY_NAME);
                Console.WriteLine("FT client retrieved directory");

                // disconnect from the server
                ftClient.Disconnect();
                Console.WriteLine("FT client disconnected");
            }
            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();
        }