netmonitor/NetMonitorServer/Service1.cs

64 lines
1.7 KiB
C#

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 client = new UdpClient(34523);
IPEndPoint iep = new IPEndPoint(IPAddress.Loopback, 0);
while (true)
{
if (Thread.CurrentThread.ThreadState == System.Threading.ThreadState.AbortRequested)
{
client.Close();
break;
}
byte[] data = client.Receive(ref iep);
string text = Encoding.UTF8.GetString(data);
switchCommand(text);
}
}
private static void switchCommand(string data)
{
if (data.Substring(0, 3).ToLower() == "net")
new SetNetworks(data);
Console.WriteLine(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);
}
}
}