/// <summary> /// 处理未捕获异常 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { SaveLog("\r\n\r\n"); SaveLog("-----------------------begin--------------------------"); SaveLog("CurrentDomain_UnhandledException " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss")); SaveLog("IsTerminating : " + e.IsTerminating.ToString()); SaveLog(e.ExceptionObject.ToString()); SaveLog("-----------------------ended--------------------------"); SaveLog("\r\n\r\n"); while (true) { //循环处理,否则应用程序将会退出 if (glExitApp) { //标志应用程序可以退出,否则程序退出后,进程仍然在运行 SaveLog("CurrentDomain_UnhandledException_ExitApp " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss")); if (FrmMainController.process_is_exit("Monitor")) { SaveLog("send RESTART_ME_RIGHTNOW to Monitor. " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss")); FrmMainController.send_data_2_monitor("RESTART_ME_RIGHTNOW"); } System.Threading.Thread.Sleep(100); //System.Environment.Exit(System.Environment.ExitCode); } SaveLog("CurrentDomain_UnhandledException_ExitApp... " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss")); System.Threading.Thread.Sleep(2 * 1000); System.Environment.Exit(-1); } ; }
/// <summary> /// 上传一个文件 /// </summary> /// <param name="strFileName">本地文件名</param> public void Put(string strFileName, MemoryStream ms) { if (!bConnected) { Connect(); } Socket socketData = CreateDataSocket(); SendCommand("STOR " + Path.GetFileName(strFileName)); if (!(iReplyCode == 125 || iReplyCode == 150)) { throw new IOException(strReply.Substring(4)); } //FileStream input = new FileStream(strFileName, FileMode.Open); //int iBytes = 0; //while ((iBytes = input.Read(buffer, 0, buffer.Length)) > 0) //{ // socketData.Send(buffer, iBytes, 0); //} //input.Close(); int iBytes = 0; while ((iBytes = ms.Read(buffer, 0, buffer.Length)) > 0) { socketData.Send(buffer, iBytes, 0); } if (socketData.Connected) { socketData.Close(); } if (!(iReplyCode == 226 || iReplyCode == 250)) { ReadReply(); if (!(iReplyCode == 226 || iReplyCode == 250)) { throw new IOException(strReply.Substring(4)); } } Logger.Trace(LogInfoType.EROR, "上传文件:" + strFileName + ",OK.", "FTP", LogCategory.I); FrmMainController.add_log_info(LogInfoType.EROR, "上传文件:" + strFileName + ",OK.", "FTP", LogCategory.I); }
/// <summary> /// 建立进行数据连接的socket /// </summary> /// <returns>数据连接socket</returns> private Socket CreateDataSocket() { SendCommand("PASV"); if (iReplyCode != 227) { throw new IOException(strReply.Substring(4)); } int index1 = strReply.IndexOf('('); int index2 = strReply.IndexOf(')'); string ipData = strReply.Substring(index1 + 1, index2 - index1 - 1); int[] parts = new int[6]; int len = ipData.Length; int partCount = 0; string buf = ""; for (int i = 0; i < len && partCount <= 6; i++) { char ch = Char.Parse(ipData.Substring(i, 1)); if (Char.IsDigit(ch)) { buf += ch; } else if (ch != ',') { throw new IOException("Malformed PASV strReply: " + strReply); } if (ch == ',' || i + 1 == len) { try { parts[partCount++] = Int32.Parse(buf); buf = ""; } catch (Exception ee) { Logger.Trace(LogInfoType.EROR, ee.Message, "FTP", LogCategory.I); FrmMainController.add_log_info(LogInfoType.EROR, ee.Message, "FTP", LogCategory.I); throw new IOException("Malformed PASV strReply: " + strReply); } } } string ipAddress = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3]; int port = (parts[4] << 8) + parts[5]; Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddress), port); try { s.Connect(ep); } catch (Exception ee) { Logger.Trace(LogInfoType.EROR, ee.Message, "FTP", LogCategory.I); FrmMainController.add_log_info(LogInfoType.EROR, ee.Message, "FTP", LogCategory.I); //throw new IOException("Can't connect to remote server"); return(null); } return(s); }
/// <summary> /// 下载一个文件 /// </summary> /// <param name="strRemoteFileName">要下载的文件名</param> /// <param name="strFolder">本地目录(不得以\结束)</param> /// <param name="strLocalFileName">保存在本地时的文件名</param> public int Get(string strRemoteFileName, string strFolder, string strLocalFileName) { if (!bConnected) { Connect(); } SetTransferType(TransferType.Binary); if (strLocalFileName.Equals("")) { strLocalFileName = strRemoteFileName; } //if (!File.Exists(strLocalFileName)) //{ // Stream st = File.Create(strLocalFileName); // st.Close(); //} FileStream output = new FileStream(strFolder + "\\" + strLocalFileName, FileMode.Create); Socket socketData = CreateDataSocket(); SendCommand("RETR " + strRemoteFileName); if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226 || iReplyCode == 250)) { //throw new IOException(strReply.Substring(4)); return(-1); } while (true) { int iBytes = socketData.Receive(buffer, buffer.Length, 0); output.Write(buffer, 0, iBytes); if (iBytes <= 0) { break; } } output.Close(); if (socketData.Connected) { socketData.Close(); } if (!(iReplyCode == 226 || iReplyCode == 250)) { ReadReply(); if (!(iReplyCode == 226 || iReplyCode == 250)) { //throw new IOException(strReply.Substring(4)); return(-1); } } Logger.Trace(LogInfoType.INFO, "下载文件:" + strRemoteFileName + ",OK.", "FTP", LogCategory.I); FrmMainController.add_log_info(LogInfoType.INFO, "下载文件:" + strRemoteFileName + ",OK.", "FTP", LogCategory.I); return(0); }
/// <summary> /// 建立连接 /// </summary> public void Connect() { string errInfo = ""; socketControl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort); // 链接 try { socketControl.Connect(ep); } catch (Exception ee) { Logger.Trace(LogInfoType.EROR, ee.Message, "FTP", LogCategory.I); FrmMainController.add_log_info(LogInfoType.EROR, ee.Message, "FTP", LogCategory.I); FrmMainController.add_log_info(LogInfoType.EROR, "Couldn't connect to remote server", "FTP", LogCategory.I); //throw new IOException("Couldn't connect to remote server"); bConnected = false; return; } IPAddress remote_ip = ((System.Net.IPEndPoint)socketControl.RemoteEndPoint).Address; int remote_port = ((System.Net.IPEndPoint)socketControl.RemoteEndPoint).Port; // 获取应答码 ReadReply(); if (iReplyCode != SERVER_READY_OK) { DisConnect(); throw new IOException(strReply.Substring(4)); } // 登陆 SendCommand("USER " + strRemoteUser); if (!(iReplyCode == REQUIRE_PASSWD || iReplyCode == LOG_INTERNET)) { CloseSocketConnect();//关闭连接 errInfo = string.Format("连接到FTP server {0}:{1} ,FAILED.", RemoteHost, strRemotePort); Logger.Trace(LogInfoType.EROR, errInfo, "FTP", LogCategory.I); FrmMainController.add_log_info(LogInfoType.EROR, errInfo, "FTP", LogCategory.I); bConnected = false; return; } if (iReplyCode != LOG_INTERNET) { SendCommand("PASS " + strRemotePass); if (!(iReplyCode == LOG_INTERNET || iReplyCode == COMMAND_NO_EXECUTE)) { CloseSocketConnect();//关闭连接 //throw new IOException(strReply.Substring(4)); errInfo = string.Format("连接到FTP server {0}:{1} ,FAILED.", RemoteHost, strRemotePort); Logger.Trace(LogInfoType.EROR, errInfo, "FTP", LogCategory.I); FrmMainController.add_log_info(LogInfoType.EROR, errInfo, "FTP", LogCategory.I); bConnected = false; return; } } errInfo = string.Format("连接到FTP server {0}:{1} ,OK.", RemoteHost, strRemotePort); bConnected = true; Logger.Trace(LogInfoType.INFO, errInfo, "FTP", LogCategory.I); FrmMainController.add_log_info(LogInfoType.INFO, errInfo, "FTP", LogCategory.I); // 切换到目录 ChDir(strRemotePath); }