943,892 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2690
  • C# RSS
Oct 10th, 2008
0

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

Expand Post »
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:


c# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dtakis is offline Offline
6 posts
since May 2007
Oct 10th, 2008
0

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

If you comment out the logging - does the service still work?
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Oct 12th, 2008
0

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

It has the same problem!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dtakis is offline Offline
6 posts
since May 2007
Oct 12th, 2008
0

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

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?
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Oct 12th, 2008
0

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

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 ...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dtakis is offline Offline
6 posts
since May 2007
Oct 12th, 2008
0

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

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Oct 13th, 2008
0

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

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dtakis is offline Offline
6 posts
since May 2007
Oct 13th, 2008
0

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

Inteaction with desktop, eg displaying a form or messagebox, trayicon etc.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Socket programming in C#
Next Thread in C# Forum Timeline: How do you draw a bar





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC