Komunikation mit server erstellt

This commit is contained in:
2010-04-26 10:46:03 +00:00
parent f4206ac81c
commit 96fe859f9a
6 changed files with 74 additions and 13 deletions
+10 -4
View File
@@ -30,7 +30,6 @@
{ {
this.components = new System.ComponentModel.Container(); this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Netzmonitor)); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Netzmonitor));
this.serviceController1 = new System.ServiceProcess.ServiceController();
this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
@@ -50,7 +49,6 @@
this.trayMenuNetzwerk = new System.Windows.Forms.ToolStripMenuItem(); this.trayMenuNetzwerk = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout(); this.menuStrip1.SuspendLayout();
this.statusStrip1.SuspendLayout(); this.statusStrip1.SuspendLayout();
this.trayMenu.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// menuStrip1 // menuStrip1
@@ -167,6 +165,15 @@
this.trayIcon.Visible = true; this.trayIcon.Visible = true;
this.trayIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.trayIcon_MouseDoubleClick); 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 // Netzmonitor
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -182,14 +189,13 @@
this.menuStrip1.PerformLayout(); this.menuStrip1.PerformLayout();
this.statusStrip1.ResumeLayout(false); this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout(); this.statusStrip1.PerformLayout();
this.trayMenu.ResumeLayout(false);
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
} }
#endregion #endregion
private System.ServiceProcess.ServiceController serviceController1;
private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2;
+1 -2
View File
@@ -16,8 +16,7 @@ namespace NetMonitorClient
{ {
InitializeComponent(); InitializeComponent();
InitNetworks(); InitNetworks();
this.serviceController1.ServiceName = "NetMonitorServer"; this.sc = new ServiceControl("NetMonitorServer");
this.sc = new ServiceControl(this.serviceController1);
} }
private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e) private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
+17 -4
View File
@@ -2,20 +2,33 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.ServiceProcess;
using System.Net.Sockets;
using System.Net;
namespace NetMonitorClient namespace NetMonitorClient
{ {
class ServiceControl class ServiceControl
{ {
private System.ServiceProcess.ServiceController sc; private UdpClient client;
public ServiceControl(System.ServiceProcess.ServiceController Controller) 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() 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) internal void setNetworkIp(string ip, string subnet, string gateway)
+6
View File
@@ -36,6 +36,7 @@
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
@@ -53,6 +54,11 @@
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Service1.resx">
<DependentUpon>Service1.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- 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. Other similar extension points exist, see Microsoft.Common.targets.
+9 -2
View File
@@ -28,10 +28,17 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
components = new System.ComponentModel.Container(); this.components = new System.ComponentModel.Container();
this.ServiceName = "Service1"; this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// Service1
//
this.ServiceName = "NetMonitorServer";
} }
#endregion #endregion
private System.Windows.Forms.Timer timer1;
} }
} }
+30
View File
@@ -6,22 +6,52 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.ServiceProcess; using System.ServiceProcess;
using System.Text; using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace NetMonitorServer namespace NetMonitorServer
{ {
public partial class Service1 : ServiceBase public partial class Service1 : ServiceBase
{ {
private Thread ot;
public Service1() public Service1()
{ {
InitializeComponent(); 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) protected override void OnStart(string[] args)
{ {
this.ot.Start();
} }
protected override void OnStop() protected override void OnStop()
{ {
this.ot.Abort();
} }
} }
} }