using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Data; using System.Data.SqlClient; using System.Threading;namespace Wrox.ProCSharp.WinServices { public class UdpServer { public static string Connection { get{return "data source=SNAIL\\SNAIL;initial catalog=Alert;password=sa;persist security info=True;user id=sa;";} } private const int listenPort = 4001; private static UdpClient listener = new UdpClient(listenPort);
static SqlConnection conn = new SqlConnection(Connection); private static Thread listenerThread; protected static void Listener() { bool done = false; IPEndPoint groupEP = new IPEndPoint(IPAddress.Any,listenPort); try { while (!done) { byte[] bytes = listener.Receive( ref groupEP); string ipstr,id,status,alertid; ipstr = groupEP.Address.ToString(); id = System.Convert.ToString(bytes[5], 16); status = System.Convert.ToString(bytes[6], 16); if (IsExsit1(ipstr,id,out alertid) && status == "1") { string selectStr = "update AlertLog set AlertDate=getdate() where AlertID ='"+alertid+"' "; SqlCommand checkin = new SqlCommand(selectStr,conn); checkin.Connection = conn; conn.Open(); checkin.ExecuteNonQuery(); conn.Close(); continue; } if(!IsExsit(ipstr,id,status,out alertid)) { string selectStr = "insert into AlertLog(AlertDate,IPAddr,RegionID,RegionStatus) values(getdate(),'"+ipstr+"','"+id+"','"+status+"')"; SqlCommand checkin = new SqlCommand(selectStr,conn); checkin.Connection = conn; conn.Open(); checkin.ExecuteNonQuery(); conn.Close(); } }
} catch (Exception e) { Console.WriteLine(e.ToString()); conn.Close(); } } private static bool IsExsit(string ipstr,string id,string status,out string alertid) { bool result = false; alertid = null; string selectStr = "select top 1 AlertID,RegionStatus from [AlertLog] where IPAddr ='"+ipstr+"' and RegionID ='"+id+"'order by AlertDate desc"; SqlCommand checkip = new SqlCommand(selectStr,conn); checkip.Connection = conn; try { conn.Open(); SqlDataReader sqlreader = checkip.ExecuteReader(); if (sqlreader.Read()) { if (sqlreader["RegionStatus"].ToString()== status) result = true; alertid = sqlreader["AlertID"].ToString(); } } catch (System.Exception e) { Console.WriteLine(e.ToString()); } conn.Close(); return result; } private static bool IsExsit1(string ipstr,string id,out string alertid) { bool result = false; alertid = null; string selectStr = "select top 1 AlertID,RegionStatus from [AlertLog] where IPAddr ='"+ipstr+"' and RegionID ='"+id+"' and RegionStatus= '1' order by AlertDate desc"; SqlCommand checkip = new SqlCommand(selectStr,conn); checkip.Connection = conn; try { conn.Open(); SqlDataReader sqlreader = checkip.ExecuteReader(); if (sqlreader.Read()) { result = true; alertid = sqlreader["AlertID"].ToString(); } } catch (System.Exception e) { Console.WriteLine(e.ToString()); } conn.Close(); return result; } public static void Start() { listenerThread = new Thread(new ThreadStart(Listener)); listenerThread.Start(); } public static void Stop() { listener.Close(); } public static void Suspend() { listenerThread.Suspend(); }
public static void Resume() { listenerThread.Resume(); } } }
这是UdpServer类库 以下是服务:
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess;namespace Wrox.ProCSharp.WinServices { public class udp : System.ServiceProcess.ServiceBase { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public udp() { // 该调用是 Windows.Forms 组件设计器所必需的。 InitializeComponent(); // TODO: 在 InitComponent 调用后添加任何初始化 } // 进程的主入口点 static void Main() { System.ServiceProcess.ServiceBase[] ServicesToRun;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Threading;namespace Wrox.ProCSharp.WinServices
{
public class UdpServer
{
public static string Connection
{
get{return "data source=SNAIL\\SNAIL;initial catalog=Alert;password=sa;persist security info=True;user id=sa;";}
}
private const int listenPort = 4001;
private static UdpClient listener = new UdpClient(listenPort);
static SqlConnection conn = new SqlConnection(Connection);
private static Thread listenerThread; protected static void Listener()
{
bool done = false;
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any,listenPort);
try
{
while (!done)
{
byte[] bytes = listener.Receive( ref groupEP);
string ipstr,id,status,alertid;
ipstr = groupEP.Address.ToString();
id = System.Convert.ToString(bytes[5], 16);
status = System.Convert.ToString(bytes[6], 16); if (IsExsit1(ipstr,id,out alertid) && status == "1")
{
string selectStr = "update AlertLog set AlertDate=getdate() where AlertID ='"+alertid+"' ";
SqlCommand checkin = new SqlCommand(selectStr,conn);
checkin.Connection = conn;
conn.Open();
checkin.ExecuteNonQuery();
conn.Close(); continue;
} if(!IsExsit(ipstr,id,status,out alertid))
{
string selectStr = "insert into AlertLog(AlertDate,IPAddr,RegionID,RegionStatus) values(getdate(),'"+ipstr+"','"+id+"','"+status+"')";
SqlCommand checkin = new SqlCommand(selectStr,conn);
checkin.Connection = conn;
conn.Open();
checkin.ExecuteNonQuery();
conn.Close();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
conn.Close();
}
} private static bool IsExsit(string ipstr,string id,string status,out string alertid)
{
bool result = false;
alertid = null;
string selectStr = "select top 1 AlertID,RegionStatus from [AlertLog] where IPAddr ='"+ipstr+"' and RegionID ='"+id+"'order by AlertDate desc";
SqlCommand checkip = new SqlCommand(selectStr,conn);
checkip.Connection = conn;
try
{
conn.Open();
SqlDataReader sqlreader = checkip.ExecuteReader();
if (sqlreader.Read())
{
if (sqlreader["RegionStatus"].ToString()== status)
result = true;
alertid = sqlreader["AlertID"].ToString();
}
}
catch (System.Exception e)
{
Console.WriteLine(e.ToString());
}
conn.Close();
return result;
} private static bool IsExsit1(string ipstr,string id,out string alertid)
{
bool result = false;
alertid = null;
string selectStr = "select top 1 AlertID,RegionStatus from [AlertLog] where IPAddr ='"+ipstr+"' and RegionID ='"+id+"' and RegionStatus= '1' order by AlertDate desc";
SqlCommand checkip = new SqlCommand(selectStr,conn);
checkip.Connection = conn;
try
{
conn.Open();
SqlDataReader sqlreader = checkip.ExecuteReader();
if (sqlreader.Read())
{
result = true;
alertid = sqlreader["AlertID"].ToString();
}
}
catch (System.Exception e)
{
Console.WriteLine(e.ToString());
}
conn.Close();
return result;
} public static void Start()
{
listenerThread = new Thread(new ThreadStart(Listener));
listenerThread.Start();
} public static void Stop()
{
listener.Close();
} public static void Suspend()
{
listenerThread.Suspend();
}
public static void Resume()
{
listenerThread.Resume();
}
}
}
以下是服务:
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;namespace Wrox.ProCSharp.WinServices
{
public class udp : System.ServiceProcess.ServiceBase
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public udp()
{
// 该调用是 Windows.Forms 组件设计器所必需的。
InitializeComponent(); // TODO: 在 InitComponent 调用后添加任何初始化
} // 进程的主入口点
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// 同一进程中可以运行多个用户服务。若要将
//另一个服务添加到此进程,请更改下行
// 以创建另一个服务对象。例如,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new udp() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// udp
//
this.ServiceName = "udp"; } /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} /// <summary>
/// 设置具体的操作,以便服务可以执行它的工作。
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
UdpServer.Start();
}
/// <summary>
/// 停止此服务。
/// </summary>
protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
UdpServer.Stop();
} protected override void OnPause()
{
UdpServer.Suspend();
} protected override void OnContinue()
{
UdpServer.Resume();
}
}
}
http://blog.csdn.net/knight94/archive/2006/08/22/1104893.aspx
再有问题如下,我在web应用程序中用ServiceController.Pause();方法调用被拒绝了 请问该怎么修改,是不是得先在哪里设置权限属性呢?