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); }
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); }
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()); }
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(); }
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(); }
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(); }
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"); }
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(); }