我做了一个关于udp监听到数据,读入到sql sever里的类库 UdpServer,用控制台测试程序调用UdpServer.Start就可以启动程序功能以及将数据读入数据库,但做到windows服务里,UdpServer.Start作为Onstart,然后启动服务,发现程序功能也在运行,因为监听端口被占用了,就是读不进数据,sql sever里没有任何数据更新,请问这是怎么回事呢,急求!

解决方案 »

  1.   

    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();
    }
    }
    }
      

  2.   

    这是UdpServer类库
    以下是服务:
      

  3.   

    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;

    // 同一进程中可以运行多个用户服务。若要将
    //另一个服务添加到此进程,请更改下行
    // 以创建另一个服务对象。例如,
    //
    //   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();
    }
    }
    }
      

  4.   

    你调试一下service,看看程序是否有问题用外挂进程进行调试看看
      

  5.   

    怎么用外挂调试 我用console程序调试UdpServer.Start() 是可以正常执行的阿
      

  6.   

    stop 就好像有问题了 因为实例重复的原因
      

  7.   

    如果允许stop,你可以借鉴我的方法,参看
    http://blog.csdn.net/knight94/archive/2006/08/22/1104893.aspx
      

  8.   

    我之前就是参考你的写的阿 你的全把功能都写入service里了 我只是试过不行 所以就写了一个类库 然后引用 奇怪 安装过程中没有任何错误 就是装成windows服务后 就执行的功能不一样了 主要区别就是写不进数据库
      

  9.   

    to 渔翁:我可能没给你说清楚,我创建服务后 在mmc里是可以启动和停止的  就是执行的功能不一样 写不进数据库
      

  10.   

    to 就是执行的功能不一样 写不进数据库看你的connectionstring的datasource部分写得很怪,用ip替换试试
      

  11.   

    ha 谢谢渔翁 还真是一改过来 就可以了 
    再有问题如下,我在web应用程序中用ServiceController.Pause();方法调用被拒绝了 请问该怎么修改,是不是得先在哪里设置权限属性呢?