I try to create a Windows Service which listens to UDP port 514, i install it using i

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2007
Posts: 5
Reputation: dtakis is an unknown quantity at this point 
Solved Threads: 0
dtakis dtakis is offline Offline
Newbie Poster

I try to create a Windows Service which listens to UDP port 514, i install it using i

 
0
  #1
Oct 10th, 2008
I try to create a Windows Service which listens to UDP port 514, i install it using installutil.exe and i get the following error message:

---------------------------
Services
---------------------------
The SyslogService2005 service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.
---------------------------
OK
---------------------------

In practise it starts working till the progressbar in Properties Window ends!

my code:


  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. //using System.Linq;
  7. using System.ServiceProcess;
  8. using System.Text;
  9. using System.Net.Sockets;
  10. using System.Net;
  11. using MySql.Data;
  12. using MySql.Data.MySqlClient;
  13. using System.Threading;
  14. using System.IO;
  15.  
  16. namespace SyslogService2005
  17. {
  18. public partial class Service1 : ServiceBase
  19. {
  20. public Service1()
  21. {
  22. InitializeComponent();
  23. this.ServiceName = "SyslogServiceUDP514";
  24. this.CanStop = true;
  25. this.CanPauseAndContinue = true;
  26. this.AutoLog = true;
  27. }
  28.  
  29. private const int listenPort = 514;
  30.  
  31. class UdpState
  32. {
  33. public UdpClient u;
  34. public IPEndPoint e;
  35. }
  36.  
  37.  
  38. public static bool messageReceived = false;
  39.  
  40. public static bool processWorking = false;
  41.  
  42. public static void ReceiveCallback(IAsyncResult ar)
  43. {
  44. UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
  45. IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
  46.  
  47.  
  48. Byte[] receiveBytes = u.EndReceive(ar, ref e);
  49. string receiveString = Encoding.ASCII.GetString(receiveBytes);
  50. LogMessageToFile(receiveString);
  51.  
  52. //Console.WriteLine("Received: {0}", receiveString);
  53. messageReceived = true;
  54.  
  55.  
  56. }
  57.  
  58.  
  59.  
  60. private static void StartListener()
  61. {
  62. // Receive a message and write it to the console.
  63. //LogMessageToFile("ReceiveMessage");
  64.  
  65. IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort);
  66. //LogMessageToFile("UdpClient");
  67. UdpClient u = new UdpClient(e);
  68. //LogMessageToFile("UdpState");
  69. UdpState s = new UdpState();
  70. //LogMessageToFile("s.e = e");
  71. s.e = e;
  72. // LogMessageToFile("s.u");
  73. s.u = u;
  74.  
  75.  
  76. //Console.WriteLine("listening for messages");
  77.  
  78. //LogMessageToFile("Start Asychronous");
  79. while (processWorking==true)
  80. {
  81. u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
  82.  
  83. // Do some work while we wait for a message. For this example,
  84. // we'll just sleep
  85. while (!messageReceived)
  86. {
  87. //LogMessageToFile("Sleep 100 ms");
  88. Thread.Sleep(100);
  89. }
  90. }
  91.  
  92.  
  93.  
  94. }
  95.  
  96.  
  97. static void LogMessageToFile(string msg)
  98. {
  99. System.IO.StreamWriter sw = System.IO.File.AppendText("C:\\Services\\LogFile.txt");
  100. try
  101. {
  102. string logLine = System.String.Format("{0:G}: {1}.", System.DateTime.Now, msg);
  103. sw.WriteLine("+----------------------------------------------------------------+");
  104.  
  105. sw.WriteLine(logLine);
  106. }
  107. finally
  108. {
  109. sw.Close();
  110. }
  111. }
  112.  
  113. protected override void OnStart(string[] args)
  114. {
  115. // TODO: Add code here to start your service.
  116. //LogMessageToFile("Service Started");
  117.  
  118. processWorking = true;
  119.  
  120. //StartListener();
  121. //ThreadStart job = new ThreadStart(StartListener);
  122. //Thread thread = new Thread(job);
  123. //thread.Start();
  124. //LogMessageToFile("StartListener Started");
  125. StartListener();
  126. }
  127.  
  128. protected override void OnStop()
  129. {
  130. // TODO: Add code here to perform any tear-down necessary to stop your service.
  131. if ((Thread.CurrentThread != null) && (Thread.CurrentThread.IsAlive))
  132. {
  133. processWorking = false;
  134. Thread.Sleep(5000);
  135. Thread.CurrentThread.Abort();
  136.  
  137. LogMessageToFile("Service Stopped");
  138.  
  139. }
  140. }
  141. }
  142. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: I try to create a Windows Service which listens to UDP port 514, i install it using i

 
0
  #2
Oct 10th, 2008
If you comment out the logging - does the service still work?
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 5
Reputation: dtakis is an unknown quantity at this point 
Solved Threads: 0
dtakis dtakis is offline Offline
Newbie Poster

Re: I try to create a Windows Service which listens to UDP port 514, i install it using i

 
0
  #3
Oct 12th, 2008
It has the same problem!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: I try to create a Windows Service which listens to UDP port 514, i install it using i

 
0
  #4
Oct 12th, 2008
Does it work if you tell it it can interact with desktop, or if you run it as you rather than a local service account?
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 5
Reputation: dtakis is an unknown quantity at this point 
Solved Threads: 0
dtakis dtakis is offline Offline
Newbie Poster

Re: I try to create a Windows Service which listens to UDP port 514, i install it using i

 
0
  #5
Oct 12th, 2008
i created a console application with the same functionality and it is working properly!
What the **** with the windows service! The service manager cannot understand that it started ...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: I try to create a Windows Service which listens to UDP port 514, i install it using i

 
0
  #6
Oct 12th, 2008
Depending on which OS and wether you're part of a domain etc, windows 2003 for example is common to have it so you cannot interact with the desktop.. if it thinks for any reaosn you might, the service doesnt work.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 5
Reputation: dtakis is an unknown quantity at this point 
Solved Threads: 0
dtakis dtakis is offline Offline
Newbie Poster

Re: I try to create a Windows Service which listens to UDP port 514, i install it using i

 
0
  #7
Oct 13th, 2008
What type of interaction? (Development is on Windows Vista)
In most tutorials in OnStart section they create just a event in EventLog or they write service started in a txt file ("Service Started") and they create another event in OnStop section... I just have more functionality in my case...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: I try to create a Windows Service which listens to UDP port 514, i install it using i

 
0
  #8
Oct 13th, 2008
Inteaction with desktop, eg displaying a form or messagebox, trayicon etc.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC