windows service监听端口

leekeeny 2010-04-12 02:58:23
怎么去做一个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();
}

}
}
...全文
314 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cs_winnerchen 2010-11-17
  • 打赏
  • 举报
回复
请问这个东西现在做好了没?

我现在也需要做个类似的。可以把相关的代码发给我吗?
邮箱:cs_winnerchen@163.com
多谢了?
oliver258 2010-04-15
  • 打赏
  • 举报
回复
友情帮顶。粗略看了下代码,应该没有什么问题
leekeeny 2010-04-15
  • 打赏
  • 举报
回复
今日再次加分。请各位帮帮忙谢谢。
leekeeny 2010-04-14
  • 打赏
  • 举报
回复
顶起。谢谢~请各位帮忙顶顶吧。帮顶的 我最后都会给分的。只要我一有分我就追加。谢谢~帮忙顶顶。问题其实不难 但是我没做过相关的东西 第一次接触。如果会的帮忙看看吧 谢谢!
leekeeny 2010-04-13
  • 打赏
  • 举报
回复
帖子已加分~请有识之士多多指教!谢谢。
我再贴出代码请大家帮忙看看吧~
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();
}

}
}
leekeeny 2010-04-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 soulsteal 的回复:]
这不就是普通的socket编程么...你只需要注意,如果你的机器是通过路由访问外网的话,记得从路由上映射下你的端口
[/Quote]貌似做过类似的吧?我没做过,可以详细点介绍吗?是外部设备接入服务器。路由怎么映射端口呢?特别是代码部分不晓得如何调试。windows service好像是调试不了的吧?
soulsteal 2010-04-12
  • 打赏
  • 举报
回复
这不就是普通的socket编程么...你只需要注意,如果你的机器是通过路由访问外网的话,记得从路由上映射下你的端口
ouzui 2010-04-12
  • 打赏
  • 举报
回复
帮顶~
leekeeny 2010-04-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 soswaidao 的回复:]
友情帮顶!!!
[/Quote]谢谢~期待高人啊~顶起!
soswaidao 2010-04-12
  • 打赏
  • 举报
回复
友情帮顶!!!

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧