怎么去做一个windows service的程序区监听一个端口呢?我是新手,之前没做过相关的东西。本机具有内网和外网。我想监听外网8000端口的数据,并一直把监听到的数据写入到一个LOG.TXT的文件里面。不知道如何实现。谁做过相关的东西可以介绍下吗?最好能贴出代码参考参考谢谢。以下是我试写的代码。请帮忙补充或改正。谢谢namespace Net_Port_Listener
{
    public partial class Service1 : ServiceBase
    {
        private TcpListener listener;
        private Thread thread;
        private TcpClient client;
        private NetworkStream nsStream; //创建接收的基本数据流
        private StreamReader srRead;        public Service1()
        {
            InitializeComponent();
        }        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            thread = new Thread(new ThreadStart(this.Listeners));
            thread.Start();
        }        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            listener.Stop();
            thread.Abort();
        }
        //多线程监听        private void Listeners()
        {
            IPAddress ip = GetServerIP();
            listener = new TcpListener(ip, 8000);
            listener.Start();            while (true)
            {
                try
                {
                    //监听是否有新的TCP连接,没有就处于堵塞状态
                    client = listener.AcceptTcpClient();
                    
                    //如果有则开启一个线程来处理
                    thread = new Thread(new ThreadStart(this.mainServer));                    thread.IsBackground = true;                    thread.Start();                }
                catch (System.Security.SecurityException)
                {
                    Console.Write("Listen faild!");
                }
                finally
                {
                    thread.Abort();
                }
            }
        }
        //Get the IPAddress
        public IPAddress GetServerIP()
        {            IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName());            return ieh.AddressList[0];        }        //处理数据主程序略
        private void mainServer()
        {
            ////
            ////
            nsStream = client.GetStream(); //获取用以发送、接收数据的网络基础数据流            srRead = new StreamReader(nsStream);//以得到的网络基础数据流来初始化StreamReader实例            string data = srRead.ReadToEnd();            int start = data.IndexOf("<dev>");            int end = data.LastIndexOf("</dev>");            string name = data.Substring(start, end);            string path = "D:/Data Log/";            WriteFile(path,data,name);
        }
        //把读取的信息写入文件!
        public static void WriteFile(string Path, string Strings, string Name)
        {
            string time = System.DateTime.Now.ToShortDateString();
            Path += time;
            Path = Path + "/" + Name + ".txt";
            if (!System.IO.File.Exists(Path))
            {
                System.IO.FileStream f = System.IO.File.Create(Path);
                f.Close();
            }
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.GetEncoding(0));
            f2.WriteLine(Strings);
            f2.Close();
            f2.Dispose();
        }    }
}

解决方案 »

  1.   

    这不就是普通的socket编程么...你只需要注意,如果你的机器是通过路由访问外网的话,记得从路由上映射下你的端口
      

  2.   

    貌似做过类似的吧?我没做过,可以详细点介绍吗?是外部设备接入服务器。路由怎么映射端口呢?特别是代码部分不晓得如何调试。windows service好像是调试不了的吧?
      

  3.   

    帖子已加分~请有识之士多多指教!谢谢。
    我再贴出代码请大家帮忙看看吧~
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    using System.Threading;
    using System.IO;namespace Net_Port_Listener
    {
        public partial class Net_Port_Listener : ServiceBase
        {
            private TcpListener listener;
            private Thread thread;
            private TcpClient client;
            private NetworkStream nsStream; //创建接收的基本数据流
            private StreamReader srRead;        public Net_Port_Listener()
            {
                InitializeComponent();
            }        protected override void OnStart(string[] args)
            {
                // TODO: 在此处添加代码以启动服务。
                thread = new Thread(new ThreadStart(this.Listeners));
                thread.Start();
            }        protected override void OnStop()
            {
                // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
                thread.Join();
                listener.Stop();
                thread.Abort();
            }
            //多线程监听        private void Listeners()
            {
                IPAddress ip = GetServerIP();
                listener = new TcpListener(ip, 8000);
                listener.Start();            while (true)
                {
                    try
                    {
                        //监听是否有新的TCP连接,没有就处于堵塞状态
                        client = listener.AcceptTcpClient();
                        
                        //如果有则开启一个线程来处理
                        thread = new Thread(new ThreadStart(this.mainServer));                    thread.IsBackground = true;                    thread.Start();                }
                    catch (System.Security.SecurityException)
                    {
                        Console.Write("Listen faild!");
                    }
                    finally
                    {
                        thread.Abort();
                    }
                }
            }
            //Get the IPAddress
            public IPAddress GetServerIP()
            {            IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName());            return ieh.AddressList[0];        }        //处理数据主程序略
            private void mainServer()
            {
                ////
                ////
                nsStream = client.GetStream(); //获取用以发送、接收数据的网络基础数据流            srRead = new StreamReader(nsStream);//以得到的网络基础数据流来初始化StreamReader实例            string data = srRead.ReadToEnd();            int start = data.IndexOf("<dev>");            int end = data.LastIndexOf("</dev>");            string name = data.Substring(start, end);            string path = "D:/Data Log/";            WriteFile(path,data,name);
            }
            //把读取的信息写入文件!
            public static void WriteFile(string Path, string Strings, string Name)
            {
                string time = System.DateTime.Now.ToShortDateString();
                Path += time;
                Path = Path + "/" + Name + ".txt";
                if (!System.IO.File.Exists(Path))
                {
                    System.IO.FileStream f = System.IO.File.Create(Path);
                    f.Close();
                }
                System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.GetEncoding(0));
                f2.WriteLine(Strings);
                f2.Close();
                f2.Dispose();
            }    }
    }
      

  4.   

    请问这个东西现在做好了没?我现在也需要做个类似的。可以把相关的代码发给我吗?
    邮箱:[email protected]
    多谢了?