using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; namespace NetMonitorServer { public partial class Service1 : ServiceBase { private Thread ot; public Service1() { InitializeComponent(); this.ot = new Thread(new ThreadStart(overwatch)); this.ot.IsBackground = true; } private static void overwatch() { UdpClient net_gets = new UdpClient(34523); IPEndPoint net_gets_port = new IPEndPoint(IPAddress.Loopback, 0); while (true) { if (Thread.CurrentThread.ThreadState == System.Threading.ThreadState.AbortRequested) { net_gets.Close(); break; } byte[] data = net_gets.Receive(ref net_gets_port); string text = Encoding.UTF8.GetString(data); switchCommand(text); } } private static void switchCommand(string data) { Console.WriteLine(data); if (data.Substring(0, 3).ToLower() == "net") new SetNetworks(data); } protected override void OnStart(string[] args) { this.ot.Start(); } protected override void OnStop() { this.ot.Abort(); } public void StartServiceConsole(string[] args) { this.OnStart(args); } } }