Browse Source

优化检测9191端口

zhouxianguang 6 years ago
parent
commit
0d564c899e
3 changed files with 48 additions and 2 deletions
  1. 2 0
      WinBox/CloseForm.Designer.cs
  2. 15 0
      WinBox/CloseForm.cs
  3. 31 2
      WinBox/MainForm.cs

+ 2 - 0
WinBox/CloseForm.Designer.cs

@@ -86,6 +86,8 @@
             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
             this.Text = "领教云课堂";
             this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
+            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CloseForm_FormClosing);
+            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.CloseForm_FormClosed);
             this.Load += new System.EventHandler(this.CloseForm_Load);
             this.ResumeLayout(false);
             this.PerformLayout();

+ 15 - 0
WinBox/CloseForm.cs

@@ -72,5 +72,20 @@ namespace WinBox
             pro.Close();
         }
 
+        private void CloseForm_FormClosing(object sender, FormClosingEventArgs e)
+        {
+          
+        }
+
+        private void CloseForm_FormClosed(object sender, FormClosedEventArgs e)
+        {
+             try
+                {
+                    RunBat("C:\\Lingjiao\\winBoxNode\\stop.bat");
+                }
+                catch (Exception ex)
+                {
+                }
+        }
     }
 }

+ 31 - 2
WinBox/MainForm.cs

@@ -9,6 +9,9 @@ using System.Windows.Forms;
 using Microsoft.Win32;
 using System.Diagnostics;
 using System.IO;
+using System.Net.NetworkInformation;
+using System.Net;
+using System.Threading;
 
 namespace WinBox
 {
@@ -66,9 +69,11 @@ namespace WinBox
                     process.StartInfo.RedirectStandardOutput = true;
                     process.Start();
                     StreamReader reader = process.StandardOutput;
+                   
                     while (!reader.EndOfStream)
                     {
                         string msg = reader.ReadLine().ToUpper();
+                        MessageBox.Show(msg);
                         if (msg.IndexOf("TCP") > 0 && msg.IndexOf(":" + tcpPort) > 0)
                         {
                             string info = msg.Trim();
@@ -90,7 +95,6 @@ namespace WinBox
             {
                 MessageBox.Show("检测TCP端口:" + tcpPort + "失败!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
             }
-
         }
 
         private void Form1_Activated(object sender, System.EventArgs e)
@@ -110,6 +114,7 @@ namespace WinBox
             pro.Start();
             pro.Close();
 
+           // detectionPort();
         }
 
         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
@@ -199,7 +204,31 @@ namespace WinBox
         private void MainForm_Shown(object sender, EventArgs e)
         {
             //检测端口
-            detectionPort();
+            //detectionPort();
+            if (!PortInUse(9191))
+            {
+                MessageBox.Show("有依赖项启动失败,请重新启动客户端或联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
+            }
+        }
+
+        public bool PortInUse(int port)
+        {
+            Thread.Sleep(2500);
+            bool inUse = false;
+
+            IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
+            IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
+
+            foreach (IPEndPoint endPoint in ipEndPoints)
+            {
+                if (endPoint.Port == port)
+                {
+                    inUse = true;
+                    break;
+                }
+            }
+
+            return inUse;
         }
     }
 }