实例介绍
【实例简介】一监听tcp程序是否正常运行的代码
这是一个检测tcp服务是否正常运行的程序如果发现程序没有正常运行就把服务重启下。
【实例截图】
【核心代码】
class Program
{
public static RedisMsgSp sp = new RedisMsgSp();
private static Timer time = new Timer();
private static string applicationPath="";
static void Main(string[] args)
{
string redisServerIp = PubClass.ReadValueByKey("RedisIp");
int redisServerPort = int.Parse(PubClass.ReadValueByKey("RedisPort"));
string strChannel = PubClass.ReadValueByKey("RedisChannel");
applicationPath = PubClass.ReadValueByKey("applicationPath");
//sp = new RedisMsgSp(redisServerIp, redisServerPort, strChannel);
//sp.ReceiveMsg();
//sp.mlCallback = new RedisMsgSp.SendEventHandler(getRedisMsg);
time.Elapsed = new ElapsedEventHandler(MethodEvent);
time.Interval = 3 * 1000;
time.Start();
Console.ReadLine();
}
public static int errortime=0;
private static void MethodEvent(object source, System.Timers.ElapsedEventArgs e)
{
bool returnstate = false;
string TcpServerIp = PubClass.ReadValueByKey("ListenIp");
int TcpServerPort = int.Parse(PubClass.ReadValueByKey("ListenPort"));
bool tcpState= TestConnection(TcpServerIp,TcpServerPort);
if (tcpState == false)
{
Console.WriteLine("Tcp服务异常第" errortime "次");
errortime ;
if (errortime >= 5)
{
Console.WriteLine("将要重启服务");
returnstate= reStartAProcess("HolterDataReceiver");
if (returnstate == true)
{
Console.WriteLine("Tcp 服务重启成功!");
errortime = 0;
}
}
}
else
{
Console.WriteLine("Tcp服务正常");
}
}
public static bool reStartAProcess(string ProcessName)
{
bool returnState = false;
Process[] myprocess = Process.GetProcessesByName(ProcessName);
if (myprocess.Count() > 0)//判断如果存在
{
int processcount = myprocess.Count();
for (int i = 0; i < processcount; i )
{
myprocess[i].Kill();//关闭程序
Console.WriteLine(ProcessName i " 程序已经关闭");
}
returnState= startProcess();
}
else
{
returnState= startProcess();
}
return returnState;
}
public static bool startProcess()
{
bool returnState = false;
try
{
Process newProcess = new Process();//创建一个新的进程
ProcessStartInfo startInfo = new ProcessStartInfo();//启动进程时使用的集合
//startInfo.FileName = Environment.CurrentDirectory "\\Release1\\a.exe";//要启动的应用程序
startInfo.FileName = applicationPath;//要启动的应用程序
startInfo.WindowStyle = ProcessWindowStyle.Normal;//启动应用程序时使用的窗口状态
//startInfo.WorkingDirectory = Environment.CurrentDirectory "\\Release1\\";//要启动应用程序的路径
newProcess.StartInfo = startInfo;//把启动进程的信息赋值给新建的进程
newProcess.StartInfo.UseShellExecute = false;//是否使用操作系统shell执行该程序
newProcess.Start();
returnState = true;
}
catch(Exception ee)
{
//退出控制台程序
returnState = false;
Console.WriteLine(ee.Message);
}
return returnState;
}
public static void getRedisMsg(msgBox mb, string sourceType)
{
Console.WriteLine(mb.msg);
}
public static SimpleTcpClient client = new SimpleTcpClient();
public static bool TestConnection(string host, int port)
{
bool returnValue = false;
try
{
var ar = client.Connect(host, port);
return ar._client.Connected;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
returnValue = false;
}
finally
{
client.Disconnect();
}
return returnValue;
}
}
.
├── App.config
├── ConsoleTestRediservice.csproj
├── Message.cs
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
├── PubClass.cs
├── RedisMsgSp.cs
├── SimpleTcpClient.cs
├── bin
│ ├── Debug
│ │ ├── ConsoleTestRediservice.exe
│ │ ├── ConsoleTestRediservice.exe.config
│ │ ├── ConsoleTestRediservice.pdb
│ │ ├── ServiceStack.Common.dll
│ │ ├── ServiceStack.Interfaces.dll
│ │ ├── ServiceStack.Redis.dll
│ │ └── ServiceStack.Text.dll
│ └── 监控服务是否正常程序
│ ├── ConsoleTestRediservice.exe
│ ├── ConsoleTestRediservice.exe.config
│ ├── ConsoleTestRediservice.pdb
│ ├── ServiceStack.Common.dll
│ ├── ServiceStack.Interfaces.dll
│ ├── ServiceStack.Redis.dll
│ └── ServiceStack.Text.dll
├── lib
│ ├── ServiceStack.Common.dll
│ ├── ServiceStack.Interfaces.dll
│ ├── ServiceStack.Redis.dll
│ ├── ServiceStack.Text.dll
│ ├── YJ.Core.dll
│ ├── YJ.Data.dll
│ ├── YJ.Utils.dll
│ ├── log4net.dll
│ └── ml_upload.dll
├── obj
│ └── Debug
│ ├── ConsoleTestRediservice.csproj.CopyComplete
│ ├── ConsoleTestRediservice.csproj.CoreCompileInputs.cache
│ ├── ConsoleTestRediservice.csproj.FileListAbsolute.txt
│ ├── ConsoleTestRediservice.csprojAssemblyReference.cache
│ ├── ConsoleTestRediservice.exe
│ ├── ConsoleTestRediservice.pdb
│ ├── DesignTimeResolveAssemblyReferencesInput.cache
│ ├── TempPE
│ ├── TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
│ ├── TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
│ └── TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
└── 好例子网_ConsoleTestRediservice.rar
8 directories, 42 files
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论