C# Windows服务监视网络
我需要构建一个Windows服务来监视网络(IP)并相应地修改代理设置 将安装该服务,并应监视IP以检测它是内部IP还是外部IP 我已经根据互联网上的指南创建了一个基本的Windows服务,但我不确定从这里开始最好的方式是什么 从指南中,我注意到C# Windows服务监视网络,c#,events,windows-services,monitoring,ip,C#,Events,Windows Services,Monitoring,Ip,我需要构建一个Windows服务来监视网络(IP)并相应地修改代理设置 将安装该服务,并应监视IP以检测它是内部IP还是外部IP 我已经根据互联网上的指南创建了一个基本的Windows服务,但我不确定从这里开始最好的方式是什么 从指南中,我注意到WindowsService对象具有某种事件系统,我想知道是否可以连接到该系统中 这是基本代码 using System; using System.Diagnostics; using System.ServiceProcess; using Syst
WindowsService
对象具有某种事件系统,我想知道是否可以连接到该系统中
这是基本代码
using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.ComponentModel;
using System.Configuration.Install;
namespace WindowsService
{
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer
{
public WindowsServiceInstaller()
{
ServiceProcessInstaller SPI = new ServiceProcessInstaller();
ServiceInstaller SI = new ServiceInstaller();
//# Service Account Information
SPI.Account = ServiceAccount.LocalSystem;
SPI.Username = null;
SPI.Password = null;
//# Service Information
SI.DisplayName = WindowsService._WindowsServiceName;
SI.StartType = ServiceStartMode.Automatic;
//# set in the constructor of WindowsService.cs
SI.ServiceName = WindowsService._WindowsServiceName;
Installers.Add(SPI);
Installers.Add(SI);
}
}
class WindowsService : ServiceBase
{
public static string _WindowsServiceName = "Serco External Proxy Manager";
public WindowsService()
{
ServiceName = _WindowsServiceName;
EventLog.Log = "Application";
// These Flags set whether or not to handle that specific
// type of event. Set to true if you need it, false otherwise.
CanHandlePowerEvent = true;
CanHandleSessionChangeEvent = true;
CanPauseAndContinue = true;
CanShutdown = true;
CanStop = true;
}
static void Main()
{
ServiceBase.Run(new WindowsService());
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
}
protected override void OnStop()
{
base.OnStop();
}
protected override void OnPause()
{
base.OnPause();
}
protected override void OnContinue()
{
base.OnContinue();
}
}
}
非常感谢您提供的任何帮助不确定是否要修改代理设置,但为了进行监视,您需要使用。您需要定义您遇到问题的零件,并将其表述为特定问题 以下是您的待办事项列表:
你的问题不清楚你已经尝试了什么,也许你可以举一个你一直遇到的问题的例子,你试图做什么来解决它,有人会提供一些帮助。我也不清楚如何修改代理设置,但至于监控网络本身,我想我能帮上忙 为了监视网络上的IP流量,您需要创建一个“原始”(或混杂)套接字。要创建这种套接字,您必须在本地框上拥有管理员权限,但只要您的Windows服务在系统帐户下运行,您就应该可以了(顺便说一句,我就是这么做的) 要创建原始套接字,请执行以下操作:
using System.Net.Sockets;
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
s.Bind(new IPEndPoint(IPAddress.Parse("192.168.0.1"), 0)); // use your local IP
byte[] incoming = BitConverter.GetBytes(1);
byte[] outgoing = BitConverter.GetBytes(1);
s.IOControl(IOControlCode.ReceiveAll, incoming, outgoing);
s.ReceiveBufferSize = 8 * 1024 * 1024; // 8MB
您现在可以使用此套接字接收指定本地IP地址上的所有传入和传出IP数据包
在Windows服务中,添加以下字段:
using System.Threading;
private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
private Thread _thread;
private Socket _socket;
在Windows服务的OnStart()
回调中,创建执行该工作的线程:
protected override void OnStart(string[] args)
{
_thread = new Thread(delegate() {
// Initialize the socket here
while (!_shutdownEvent.WaitOne(0)) {
// Receive the next packet from the socket
// Process packet, e.g., extract source/destination IP addresses/ports
// Modify proxy settings?
}
// Close socket
});
_thread.Name = "Monitor Thread";
_thread.IsBackground = true;
_thread.Start();
}
在Windows服务的OnStop()
回调中,您需要向线程发出关闭信号:
protected override void OnStop()
{
_shutdownEvent.Set();
if (!_thread.Join(3000)) { // give the thread 3 seconds to stop
_thread.Abort();
}
}
希望这能为您提供足够的入门知识。System.Net命名空间也能处理此问题-不确定WMI?我更喜欢事件,而不是使用System.Net进行轮询,因为这需要很小的资源。我没有尝试太多,因为我不知道从哪里开始,我不确定在服务中最好的方式是什么,是否是后台线程,我可以使用什么事件,如上所述,WMI可能更好。我不是在寻找关于代码的建议,这只是一个参考,你可以看到我在哪里,我是在从这一点上寻找一个方向。我意识到这并不能直接回答你的问题。但这可能会有所帮助。。。与其创建自己的代码来更改代理设置,也许您可以使用?谢谢,但我认为有些限制可能会导致我们公司网络出现问题,例如我们将浏览器个人保留给用户他不想读取数据包,但可以确定机器是否有LAN IP。我的猜测是,当他连接到一个局域网时,它应该自动配置一个代理服务器——但当用户离开局域网时不使用它。