Komunikation mit server erstellt

This commit is contained in:
BlubbFish 2010-04-26 10:46:03 +00:00
parent f4206ac81c
commit 96fe859f9a
6 changed files with 74 additions and 13 deletions

View File

@ -30,7 +30,6 @@
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Netzmonitor));
this.serviceController1 = new System.ServiceProcess.ServiceController();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
@ -50,7 +49,6 @@
this.trayMenuNetzwerk = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.statusStrip1.SuspendLayout();
this.trayMenu.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
@ -167,6 +165,15 @@
this.trayIcon.Visible = true;
this.trayIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.trayIcon_MouseDoubleClick);
//
// trayMenu
//
this.trayMenu.Name = "trayMenu";
//
// trayMenuNetzwerk
//
this.trayMenuNetzwerk.Name = "trayMenuNetzwerk";
this.trayMenuNetzwerk.Size = new System.Drawing.Size(32, 19);
//
// Netzmonitor
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -182,14 +189,13 @@
this.menuStrip1.PerformLayout();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.trayMenu.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.ServiceProcess.ServiceController serviceController1;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2;

View File

@ -16,8 +16,7 @@ namespace NetMonitorClient
{
InitializeComponent();
InitNetworks();
this.serviceController1.ServiceName = "NetMonitorServer";
this.sc = new ServiceControl(this.serviceController1);
this.sc = new ServiceControl("NetMonitorServer");
}
private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)

View File

@ -2,20 +2,33 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Net.Sockets;
using System.Net;
namespace NetMonitorClient
{
class ServiceControl
{
private System.ServiceProcess.ServiceController sc;
public ServiceControl(System.ServiceProcess.ServiceController Controller)
private UdpClient client;
private IPEndPoint rep; //remoteEndPoint
public ServiceControl(string name)
{
this.sc = Controller;
ServiceController sc = new ServiceController();
sc.ServiceName = name;
//if (sc.Status != ServiceControllerStatus.Running)
//{
// sc.Start();
//}
this.client = new UdpClient();
this.rep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 34523);
}
internal void setNetworkAuto()
{
throw new NotImplementedException();
byte[] data = Encoding.UTF8.GetBytes("NET AUTO");
this.client.Send(data, data.Length, this.rep);
//throw new NotImplementedException();
}
internal void setNetworkIp(string ip, string subnet, string gateway)

View File

@ -36,6 +36,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@ -53,6 +54,11 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Service1.resx">
<DependentUpon>Service1.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -28,10 +28,17 @@
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// Service1
//
this.ServiceName = "NetMonitorServer";
}
#endregion
private System.Windows.Forms.Timer timer1;
}
}

View File

@ -6,22 +6,52 @@ 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;
this.ot.Start();
}
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)
{
Console.WriteLine(data);
}
protected override void OnStart(string[] args)
{
this.ot.Start();
}
protected override void OnStop()
{
this.ot.Abort();
}
}
}