/// <summary> /// 初始化控件 /// </summary> void initializeWidget() { Dictionary <string, string> configDict = ConfigFile.loadConfig(); if (configDict == null) { MessageBox.Show("无法加载配置文件!"); return; } try { textBox_FtpServer.Text = configDict[ConfigFile.FTPServer]; textBox_User.Text = configDict[ConfigFile.User]; textBox_Password.Text = configDict[ConfigFile.Password]; numericUpDown_CtrlPort.Value = int.Parse(configDict[ConfigFile.CtrlPort]); numericUpDown_DataPort.Value = int.Parse(configDict[ConfigFile.DataPort]); } catch (Exception e) { BugReport.report(e); } button_disconnect.Enabled = false; button_Download.Enabled = false; button_Upload.Enabled = false; }
/// <summary> /// 加载配置文件 /// </summary> /// <returns></returns> public static Dictionary <string, string> loadConfig() { try { Dictionary <string, string> configContent = new Dictionary <string, string>(); StreamReader configReader = new StreamReader(CONFIG_PATH, Encoding.UTF8); string line = ""; string item = ""; string value = ""; while ((line = configReader.ReadLine()) != null) { item = CodeAnalysis.getCommandString(line); value = CodeAnalysis.getValueString(line)[0]; if (!string.IsNullOrEmpty(item) && !configContent.ContainsKey(item)) { configContent.Add(item, value); } } configReader.Close(); return(configContent); } catch (Exception e) { BugReport.report(e); return(null); } }
/// <summary> /// 接收服务器的消息时的处理 /// </summary> /// <param name="iar"></param> void receiveCallBack(IAsyncResult iar) { try { int REnd = ctrlSocket.EndReceive(iar); string receive_content = Encoding.ASCII.GetString(receiveByte, 0, REnd); Reporter.BugReport.reportFTPLog(receive_content); richTextBoxConsole.Invoke(new MethodInvoker(delegate { richTextBoxConsole.AppendText(DateTime.Now.ToString() + "\n" + receive_content + "\n"); })); /* PASSIVE模式传输文件,获取传输端口号 */ if (receive_content.Contains("227 Entering Passive Mode")) { int startIndex = receive_content.IndexOf('(') + 1; string param_s = receive_content.Substring(receive_content.IndexOf('(') + 1, receive_content.Length - startIndex - 3); /* * richTextBoxConsole.Invoke(new MethodInvoker(delegate * { * richTextBoxConsole.AppendText(DateTime.Now.ToString() + "\n" + param_s + "\n"); * }));*/ int num1 = int.Parse(CodeAnalysis.getValueString("port_param " + param_s)[4]); int num2 = int.Parse(CodeAnalysis.getValueString("port_param " + param_s)[5]); int dataPort = num1 * 256 + num2; richTextBoxConsole.Invoke(new MethodInvoker(delegate { richTextBoxConsole.AppendText(DateTime.Now.ToString() + "\n" + "get data port : " + dataPort.ToString() + "\n"); })); lock (ServerIP) { /* 启动数据传输进程 */ if (IsUpload) { CMDMethod.CMDComand.executeDataTransferProcess(ServerIP, dataPort, FileName); } else { CMDMethod.CMDComand.executeDataDownloadProcess(ServerIP, dataPort, FileName, FileSize); } } } /* 获取文件尺寸 */ else if (receive_content.Contains("213 ")) { FileSize = int.Parse(CodeAnalysis.getValueString(receive_content)[0]); } ctrlSocket.BeginReceive(receiveByte, 0, receiveByte.Length, 0, asyncCallback, null); } catch (Exception e) { BugReport.report(e); } }
/// <summary> /// 接收服务器的消息时的处理 /// </summary> /// <param name="iar"></param> void receiveCallBack(IAsyncResult iar) { try { if (receiveCounter >= FileSize) { fs.Write(receiveByte, 0, FileSize % receiveByte.Length); fs.Close(); } else { fs.Write(receiveByte, 0, receiveByte.Length); receiveCounter += receiveByte.Length; dataSocket.BeginReceive(receiveByte, 0, receiveByte.Length, 0, receiveCallBack, null); } } catch (Exception e) { BugReport.report(e); } }
static void Main(string[] args) { try { Program program = new Program(); string ip = args[0]; int port = int.Parse(args[1]); string fileName = args[2]; BugReport.report(new Exception(ip + " " + port.ToString() + " " + fileName)); Console.WriteLine("server ip : " + ip); Console.WriteLine("port : " + port.ToString()); program.dataConnect(ip, port, fileName); Thread.Sleep(50); program.dataDisconnect(); } catch (Exception e) { Console.Write(e.ToString()); Console.WriteLine(); BugReport.report(e); } //Console.ReadLine(); }
/// <summary> /// 保存配置 /// </summary> /// <param name="configContent"></param> public static void saveConfig(Dictionary <string, string> configContent) { if (configContent == null) { return; } try { StreamWriter configWriter = new StreamWriter(CONFIG_PATH, false, Encoding.UTF8); if (configContent.ContainsKey(FTPServer)) { configWriter.WriteLine(FTPServer + " " + configContent[FTPServer]); } if (configContent.ContainsKey(CtrlPort)) { configWriter.WriteLine(CtrlPort + " " + configContent[CtrlPort]); } if (configContent.ContainsKey(DataPort)) { configWriter.WriteLine(DataPort + " " + configContent[DataPort]); } if (configContent.ContainsKey(User)) { configWriter.WriteLine(User + " " + configContent[User]); } if (configContent.ContainsKey(Password)) { configWriter.WriteLine(Password + " " + configContent[Password]); } configWriter.Close(); } catch (Exception e) { BugReport.report(e); } }