110,539
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
}
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();
}
}
}