C# 在c中作为Windows服务运行时,TCP/IP套接字未从计算机读取数据#
我有一个简单的TCP/IP程序从机器上读取数据并将其写入文本文件。我想把它作为Windows服务运行,这样数据就可以连续写入文本文件而不需要任何干预。现在,当我尝试在Visual Studio的调试模式下运行该程序时,它正在从计算机读取数据并保存到文本文件中,但当我将其添加为Windows服务并尝试启动该服务时,它会给出以下错误消息C# 在c中作为Windows服务运行时,TCP/IP套接字未从计算机读取数据#,c#,multithreading,sockets,tcp,windows-services,C#,Multithreading,Sockets,Tcp,Windows Services,我有一个简单的TCP/IP程序从机器上读取数据并将其写入文本文件。我想把它作为Windows服务运行,这样数据就可以连续写入文本文件而不需要任何干预。现在,当我尝试在Visual Studio的调试模式下运行该程序时,它正在从计算机读取数据并保存到文本文件中,但当我将其添加为Windows服务并尝试启动该服务时,它会给出以下错误消息 windows service could not start the service on the local computer error:1053 The
windows service could not start the service on the local computer
error:1053 The service did not respond to the start or the control request in a timely fashion
这是我的主要应用程序代码
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
这是我使用TCP/IP与机器通信并将数据读/写到文本文件中的代码
protected override void OnStart(string[] args)
{
ipaddress = "";
int port = int.Parse("");
textfileSaveLocation = "";
byte[] data = new byte[1024];
string stringData;
IPAddress ipadd = IPAddress.Parse(ipaddress);
IPEndPoint ipend = new IPEndPoint(ipadd, port);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.NoDelay = false;
try
{
sock.Connect(ipend);
}
catch (Exception dfg)
{
return;
}
try
{
buf = String.Format("SMDR", "PCCSMDR");
bBuf = Encoding.ASCII.GetBytes(buf);
sock.Send(bBuf);
while (true)
{
data = new byte[1024];
int recv = sock.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
string df = "";
try
{
FileStream dr = new FileStream(textfileSaveLocation, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
StreamReader fg = new StreamReader(dr);
df = fg.ReadToEnd();
fg.Dispose();
dr.Dispose();
}
catch (Exception dfjdfs)
{
}
try
{
FileStream cd = new FileStream(textfileSaveLocation, FileMode.Create);
StreamWriter cdf = new StreamWriter(cd);
cdf.WriteLine(df);
cdf.WriteLine(stringData);
cdf.Dispose();
cd.Dispose();
}
catch (Exception hgy)
{
}
}
sock.Shutdown(SocketShutdown.Both);
sock.Close();
}
catch (Exception DFGFD)
{
}
}
我使用这个程序的唯一目的是通过安装程序将其作为Windows服务运行并启动服务。一旦服务启动,它应该从机器的给定ip和端口读取数据,并将其保存到文本文件中。我还需要这个服务来持续监控来自机器的数据,一旦新数据进入机器,它应该读写到文本文件中
我需要在程序中实现多线程吗?系统希望
OnStart
方法能够及时返回(~30秒),因此长时间运行的任务(如数据监控)需要移动到另一个线程。这可以简单到:
private System.Threading.Thread _thread;
protected override void OnStart(string[] args)
{
_thread = new Thread(DoWork);
_thread.Start();
}
private void DoWork()
{
// create and monitor socket here...
}
请注意,虽然while(true)
循环足以保持线程运行,但在服务停止时很难停止它。对于这一点,我使用了一个类似的方法
我同意马特的回答 另外,一般来说,最好添加一个选项来调试您的服务,下面是一个示例:
protected override void OnStart(string[] args)
{
bool launchDebugger = false;
if (args.Count() > 0)
{
launchDebugger = args[0].ToLower() == "debug";
}
if (launchDebugger)
{
Debugger.Launch();
}
}
当您使用参数“debug”运行服务时,它将触发一个假异常,
允许您使用visual studio调试服务 我将对其进行测试,并将其标记为答案。谢谢。如果需要调试该服务,请调用
System.Diagnostics.Debugger.Launch()
insideMain
或OnStart
。启动服务时,系统会提示您从那里启动正常的调试会话。我实现了您的建议,并在调试模式下对其进行了测试。在调试模式下,它是一个工作文件,但当我使用visual studio创建安装安装文件并从那里安装并启动时,没有数据被转储到文本文件中..我是否需要添加这些行int recv=sock.Receive(数据);stringData=Encoding.ASCII.GetString(数据,0,recv);在while(!\u shutdownEvent.WaitOne(0))循环之前,使其作为Windows service live模式运行。该代码的位置在调试模式或live模式之间不应重要。当您在实时模式下运行时,您的工作目录是否在您认为的位置?您是否尝试过使用我上面的建议调试服务?是的,先生,工作目录仅在那里。我需要添加int recv=sock.Receive(数据);stringData=Encoding.ASCII.GetString(数据,0,recv);在(!\u shutdownEvent.WaitOne(0))循环以使其作为Windows service live Mode运行之前,接受异常不会使调试更容易。。。
protected override void OnStart(string[] args)
{
bool launchDebugger = false;
if (args.Count() > 0)
{
launchDebugger = args[0].ToLower() == "debug";
}
if (launchDebugger)
{
Debugger.Launch();
}
}