惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 沉默天蝎的学习汇集

由浅入深:Python 中如何实现自动导入缺失的库?(转) itchat初步解读登录(转) 转:【开源必备】常用git命令 2.转发。基于itchat的微信消息同步机器人 1、初学探讨PYTHON的itchat和wxpy两库 学习git 新手。这个写的不错 转:android学习帖子大汇集 bat 切换单位和宿舍IP地址(转) (转)C#2Android [转]HTTP调试工具:Fiddler的使用方法介绍 使用VS2005的 ClickOnce 技术实现按需下载组件 C# WinForm开发系列 - 文章索引 (转自peterzb) 非常经典的网络蜘蛛示例,我是转载在这里的 - 沉默天蝎的学习汇集 - 博客园 (转)三种模拟自动登录和提交POST信息的实现方法 (转自Timon's wang blogs)C#实现web信息自动抓取 (转)c#操作注册表大全 - 沉默天蝎的学习汇集 - 博客园 sql2005安装产生问题解决汇集 (转自斯克迪亚的博客)学柯南,用电脑拨电话! 看完最新的柯南后很好奇,正好有人写到了就转来了 转贴:VS2008如何转换为VS2005
转帖:麻雀虽小,五脏俱全-C# 创建windows服务、socket通讯实例
沉默天蝎的学习汇集 · 2010-08-12 · via 博客园 - 沉默天蝎的学习汇集

转:http://www.cnblogs.com/xingshao/archive/2010/08/12/1798188.html

一个windows 服务结合 Soctket、邮件发送、短信发送的小应用。功能不大,但是用到的.net下的知识点很多,我感觉对初步接触这几个相关知识的朋友还是有学习意义的。

这个功能来源于一个监视文件夹的需求:

1、 轮询监视文件夹内文件的状态

2、 在发现异常后通过邮件和短信的方式通知管理员。

3、 在有外网环境下使用邮件方式通知。

4、 无外网情况下,通过局域网中一台带有的短信设备电脑发送短信提醒。

在经过具体的功能分析后,将功能划分为4个小功能单元:

1、  运用c#开发windows服务来实现对文件夹的轮询监视。

2、  引用.net框架内的system.net.mail 命名控件的下的邮件处理类来实现邮件的发送。

3、  局域网内采用scoket解决局域网内短信设备计算机与监控计算机之间的通讯问题。

4、  C#对短信设备进行简单必要的二次开发。

一、windows服务实现轮询监视

Windows 服务,以前的NT服务,都是被作为Windows NT操作系统的一部分引进来的。你需要使用NT级别的操作系统才可运行Windows服务,诸如:Windows NT、Windows 2000 Professional、windows XP或Windows 2000 Server以上操作系统。举例而言,以Windows服务形式的产品有:Microsoft Exchange、SQL Server,还有别的如设置计算机时钟的Windows Time服务。它随 Windows 操作系统启动而启动的,在后台运行的,通常不和用户产生交互的程序。

在.net框架下创建Windows 服务非常的简单快捷,它封装了Windows服务程序的创建和控制过程,程序相关的命名空间涉及到以下两个:System.ServiceProcess和System.Diagnostics。

    首先是创建windows服务,在.net框架中直接创建windows服务项目即可,在服务启动事件中添加必要的逻辑处理。〔实现见下代码〕

protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
//读取配置信息
ReadConfig();

timer1.Elapsed

+=new System.Timers.ElapsedEventHandler(timer1_Elapsed);
timer1.Interval
= GlobalInfo.TimeInterval;
timer1.Start();
}
void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{

DirectoryInfo dirs

= new DirectoryInfo(GlobalInfo.DirName);
int count=0;
switch (GlobalInfo.Type)
{
case 0://延迟时间
foreach (FileInfo var in dirs.GetFiles())
{
DateTime createTime
= var.CreationTime;
if (System.DateTime.Now > createTime.AddHours(GlobalInfo.Size))
count
++;
}
if (count > 0)
MessageSender.Send(GlobalInfo.Norm
> 1 ? false : true, string.Format("{0}个数据文件延迟超过{1}小时,请关注。[报告时间:{2}]", count.ToString(), GlobalInfo.Size.ToString(), System.DateTime.Now.ToString()));
break;
case 1://延时数据包个数
count = dirs.GetFiles().Length;
if (count > GlobalInfo.Count)
MessageSender.Send(GlobalInfo.Norm
> 1 ? false : true, string.Format("同时有{0}个数据文件滞留,请关注。[报告时间:{1}]", count.ToString(), System.DateTime.Now.ToString()));
break;
default:
break;
}
}

二、邮件发送功能

在以前的一篇文章中提到过邮件类的使用,所以就不在作详细说明了。〔附代码:见下〕

01 static void  Send(string titel,string megHtml,string subject)
03           string meg = "";
05           string smtpAuthUsername = GlobalInfo.SenderAddr;
07           string smtpAuthPassword = GlobalInfo.SenderPwd;
09           string smtpServer = GlobalInfo.SmtpServer;
10           string objEmail = GlobalInfo.ObjAddr;
12           System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(smtpServer);
14           smtp.Credentials = new System.Net.NetworkCredential(smtpAuthUsername, smtpAuthPassword);
15           //异步发送完成获取发送状态 
16           smtp.SendCompleted += new System.Net.Mail.SendCompletedEventHandler(SendCompletedCallback);
20               System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
21               mail.From = new System.Net.Mail.MailAddress(smtpAuthUsername, smtpAuthUsername);
22               //回复人,回复人名 
23               mail.ReplyTo = new System.Net.Mail.MailAddress(smtpAuthUsername, smtpAuthUsername);
25               mail.To.Add(objEmail);
27               mail.Priority = System.Net.Mail.MailPriority.Normal;
28               //设置html邮件 
29               mail.IsBodyHtml = true;
31               mail.Subject = titel;
33               mail.Body = megHtml;
34               smtp.Send(mail);
36               meg = string.Format("{0} {1}邮件发送成功。", System.DateTime.Now.ToString(), objEmail);
38               WriteLog(meg);
42               meg = string.Format("{0} {1}邮件发送失败。", System.DateTime.Now.ToString(), objEmail);
44               WriteLog(meg);

三、局域网内Socket通讯

  Socket原意是“插座”。应用层通过传输层进行数据通信时,TCP和UDP会遇到同时为多个应用程序进程提供并发服务的问题。多个TCP连接或多个应用程序进程可能需要通过同一个TCP协议端口传输数据。为了区别不同的应用程序进程和连接,许多计算机操作系统为应用程序与TCP/IP协议交互提供了称为套接字(Socket)的接口,区分不同应用程序进程间的网络通信和连接。 生成套接字,主要有3个参数:通信的目的IP地址、使用的传输层协议(TCP或UDP)和使用的端口号。通过将这3个参数结合起来,与一个“插座”Socket绑定,应用层就可以和传输层通过套接字接口,区分来自不同应用程序进程或网络连接的通信,实现数据传输的并发服务。 Socket可以看成在两个程序进行通讯连接中的一个端点,一个程序将一段信息写入Socket中,该Socket将这段信息发送给另外一个Socket中,使这段信息能传送到其他程序中。

  因为现有的短信设备位与局域网内的另外一台服务器上,所以需要在短信通知状态下,需要将信息发送到局域网内另一台电脑。用socket通讯的话需要一个接收信息的客户端。并且客户端需要一些参数的设置。比如:接收电话号码、开机启动、通讯计算机IP、等。〔信息接收代码见下〕

01 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
02             socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); 
05                 socket.Bind(new IPEndPoint(IPAddress.Parse(Info.IP), int.Parse(Info.PORT)));
06                 socket.Listen((int)SocketOptionName.MaxConnections);
07                 while (true)
09                     Socket a = socket.Accept();
10                     if (a.Connected)
12                         byte[] stream = new byte[80];
13                         a.Receive(stream);
14                         string message = System.Text.Encoding.UTF8.GetString(stream);
15                         InsertRechText ins = new InsertRechText(Insert);
16                         Invoke(ins, new object[] { message });
18                     if (isover)
19                         return;
22             catch (Exception ex)
24                 WriteLog(string.Format("接收信息失败。[{0}]", ex.Message));
25                 throw ex;
29                 socket.Close();

四、短信设备二次开发

短信设备用的是人大金仓的DG-C1A 短信猫。该硬件有对应的二次开发类库,开发很简单。直接将引用方法封装成一个类的静态方法。用的时候直接调用就可以了。(直接贴出代码)

01   [DllImport("GSMMultiPort.dll",
02              EntryPoint = "GSMModemInit",
03              CharSet = CharSet.Ansi,
04              CallingConvention = CallingConvention.StdCall)]
05         public static extern bool GSMModemInit(
06             string device,
07             string baudrate,
08             string initstring,
09             string charset,
10             bool swHandshake,
11             string sn);
13         [DllImport("GSMMultiPort.dll",
14              EntryPoint = "GSMModemSMSsend",
15              CharSet = CharSet.Ansi,
16              CallingConvention = CallingConvention.StdCall)]
17         public static extern bool GSMModemSMSsend(
18             string device,
19             string serviceCenterAddress,
20             int encodeval,
21             string text,
22             int textlen,
23             string phonenumber,
24             bool requestStatusReport);
26         [DllImport("GSMMultiPort.dll",
27              EntryPoint = "GSMModemGetErrorMsg",
28              CharSet = CharSet.Ansi,
29              CallingConvention = CallingConvention.StdCall)]
30         public static extern string GSMModemGetErrorMsg(string device);

在完成功能后,部署,运行效果不错。感觉这个功能虽然很小。但是用到的知识可不少,感觉还是有所收获的。毕竟术业有专攻,每个人在从事的行业开发中专注的技术也是有限的。多学点各个方面的知识对自己的成长还是很有好处的。哈哈.〔啰嗦了〕。共同学习吧