WindowsService监听端口

主程序代码

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

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值