protected override void OnStart(string[] args) { ////当用户试图重启或关闭系统时发生,但是有了OnShutdown()的重载,就可以免去该操作! //SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding); Logs.Create("开始启动监听服务..."); timer1 = new Timer(60000);//关机轮询间隔时间为1分钟 timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed); timer1.Start(); IPAddress LocalIP = IPAddress.Loopback; int LocalPort = 8000; string dir = KellFileTransfer.Common.GetAppSettingConfig("dir"); string ip = KellFileTransfer.Common.GetAppSettingConfig("ip"); string port = KellFileTransfer.Common.GetAppSettingConfig("port"); if (!string.IsNullOrEmpty(ip)) { LocalIP = IPAddress.Parse(ip); } if (!string.IsNullOrEmpty(port)) { LocalPort = int.Parse(port); } KellFileTransfer.FileUploader.ReceiveFinished += new EventHandler <KellFileTransfer.ReceiveFinishArgs>(FileTransfer_ReceiveFinished); rl = KellFileTransfer.FileUploader.StartReceiveFile(dir, LocalIP, LocalPort); if (rl.Thread.ThreadState == System.Threading.ThreadState.Running && rl.Listener != null) { Logs.Create("监听成功!"); } else { Logs.Create("监听失败!"); } }
private void StartListen() { if (textBox1.Text.Trim() == "") { string ip = KellFileTransfer.Common.GetAppSettingConfig("ip"); if (string.IsNullOrEmpty(ip)) { IPHostEntry ieh = Dns.GetHostEntry(Dns.GetHostName()); IPAddress LocalIP = IPAddress.Loopback; for (int i = 0; i < ieh.AddressList.Length; i++) { if (ieh.AddressList[i].AddressFamily == AddressFamily.InterNetwork) { LocalIP = ieh.AddressList[i]; break; } } textBox1.Text = LocalIP.ToString(); } else { textBox1.Text = ip; } } if (textBox2.Text.Trim() == "") { string port = KellFileTransfer.Common.GetAppSettingConfig("port"); if (string.IsNullOrEmpty(port)) { textBox2.Text = port; } else { textBox2.Text = "8000"; } } Logs.Create("开始监听..."); KellFileTransfer.FileUploader.ReceiveFinished += new EventHandler <KellFileTransfer.ReceiveFinishArgs>(FileTransfer_ReceiveFinished); rl = KellFileTransfer.FileUploader.StartReceiveFile(textBox3.Text, IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text.Trim())); if (rl.Thread.ThreadState == System.Threading.ThreadState.Running && rl.Listener != null) { toolStripStatusLabel1.Text = "正在监听端口[" + textBox2.Text.Trim() + "]上的客户端连接..."; checkBox1.Enabled = button1.Enabled = textBox1.Enabled = textBox2.Enabled = textBox3.Enabled = false; button3.Enabled = true; Logs.Create("监听成功!"); } else { Logs.Create("监听失败!"); } }
protected override void OnStart(string[] args) { triggerTimer = new System.Timers.Timer(); // 循环间隔时间(1分钟) triggerTimer.Interval = 60000; triggerTimer.Elapsed += new ElapsedEventHandler(triggerTimer_Elapsed); triggerTimer.Start(); ThreadPool.QueueUserWorkItem( delegate { string dir = ConfigurationManager.AppSettings["dir"]; if (string.IsNullOrEmpty(dir) || !Directory.Exists(dir)) { dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); } if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } KellFileTransfer.Common.SaveAppSettingConfig("dir", dir); IPEndPoint ipepUpload = KellFileTransfer.Common.GetUploadIPEndPoint(); IPEndPoint ipepDownload = KellFileTransfer.Common.GetDownloadIPEndPoint(); try { rl = KellFileTransfer.FileUploader.StartReceiveFile(dir, ipepUpload.Address, ipepUpload.Port); if (rl.Thread.ThreadState != System.Threading.ThreadState.Running || rl.Listener == null) { WriteLog.CreateLog("服务程序", "StartReceiveFile", "error", "开启上传监听服务失败![" + ipepUpload.ToString() + "]"); } KellFileTransfer.FileDownloadServer server = new KellFileTransfer.FileDownloadServer(dir); if (!server.StartDownloadListen(ipepDownload.Address, ipepDownload.Port)) { WriteLog.CreateLog("服务程序", "StartDownloadListen", "error", "开启下载监听服务失败![" + ipepDownload.ToString() + "]"); } } catch (Exception e) { WriteLog.CreateLog("服务程序", "鼎峰健身服务OnStart", "error", e.ToString()); } }); WriteLog.CreateLog("服务程序", "鼎峰健身服务", "log", "服务启动..."); }