944,117 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 17411
  • C# RSS
Apr 2nd, 2006
0

Windows service and threads and sockets

Expand Post »
I'm playing around with c# cos I was bored and I wanted to have a go at making a simple windows service that spits out a quote of the day if you telnet on a certain port.

I have it working but I'm not sure of the best way to stop the service. The OnStart method creates a thread and starts it. The method the thread executes is a permanent loop while(true){ create tcp listener etc..}

What is the cleanest way to cause the loop to break and the thread to return from the OnStop method of my service class?
Similar Threads
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Apr 2nd, 2006
0

Re: Windows service and threads and sockets

Ok I added a private member variable:

C# Syntax (Toggle Plain Text)
  1. Private bool RUNNING;

Set this to true in the OnStart method of the service class. Set it to false in the OnStop method.

Set the while loop to test the value of RUNNING:

C# Syntax (Toggle Plain Text)
  1. while(RUNNING){
  2. Socket s = this.l.AcceptSocket();
  3.  
  4. Char[] carr = quotes[numServed%2].ToCharArray();
  5. Byte[] barr = Encoding.ASCII.GetBytes(carr);
  6.  
  7. s.Send(barr, barr.Length, 0);
  8. s.Shutdown(SocketShutdown.Both);
  9. s.Close();
  10.  
  11. numServed++;
  12. }

That left just one last problem, the loop is stuck waiting in the AcceptSocket method of the TcpListener, so even when the value of RUNNING is set false, it doesn't get tested until one further connection is made after the service is stopped.

I imagine the elegant way is to signal the waiting socket somehow, but the easy way is to have the OnStop method issue a connection of it's own to its own listening socket to trip the loop and have it evaluate false and exit the thread. Thus:

C# Syntax (Toggle Plain Text)
  1. protected override void OnStop()
  2. {
  3. // TODO: Add tear-down code here (if required) to stop your service.
  4.  
  5. this.RUNNING = false;
  6.  
  7. //Make a connection to force an iteration in method qotdSocketOpen()
  8. Socket socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  9. IPEndPoint remote = new IPEndPoint(ip, port);
  10. socClient.Connect(remote);
  11. socClient.Shutdown(SocketShutdown.Both);
  12. socClient.Close();
  13.  
  14. }

Problem Solved !
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Apr 5th, 2006
0

Re: Windows service and threads and sockets

Hi,

You solved almost both of your questions. You may also use asynchronous socket server connection waiting using delegates.

Loren Soth
Reputation Points: 28
Solved Threads: 4
Posting Whiz in Training
Lord Soth is offline Offline
233 posts
since Mar 2006
Jun 24th, 2008
0

Re: Windows service and threads and sockets

Click to Expand / Collapse  Quote originally posted by Lord Soth ...
Hi,

You solved almost both of your questions. You may also use asynchronous socket server connection waiting using delegates.

Loren Soth
Can you please teach me that?
I really need to have a windows service with an async socket in it.

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
masihyeganeh is offline Offline
1 posts
since Jun 2008
Jul 7th, 2008
0

Re: Windows service and threads and sockets

You can always call Abort on the thread.

C# Syntax (Toggle Plain Text)
  1. m_thread.Abort();

If you need to do any cleanup, put a try-catch around your while loop.
Reputation Points: 14
Solved Threads: 5
Newbie Poster
$dunk$ is offline Offline
23 posts
since Mar 2008
Nov 10th, 2010
0

TCP Service Polling method

You can always use a polling methodology:

c# Syntax (Toggle Plain Text)
  1. private void TcpListen()
  2. {
  3. try
  4. {
  5. LogMan.Report("Starting Listener on port {0}.", SettingsManager.ListenerPort);
  6.  
  7. TcpListener listener = new TcpListener(IPAddress.Any, SettingsManager.ListenerPort);
  8. listener.Start();
  9.  
  10. while (true)
  11. {
  12. if (_stop)
  13. break;
  14.  
  15. if (!_pause)
  16. {
  17. if (listener.Pending())
  18. {
  19. TcpClient client = listener.AcceptTcpClient();
  20. LogMan.Report("Client Connected to Listener.");
  21. Thread thread = new Thread(new ParameterizedThreadStart(HandleClientConnection));
  22. thread.Start(client);
  23. }
  24. }
  25. Thread.Sleep(100);
  26. }
  27. }
  28. catch (Exception ex)
  29. {
  30. LogMan.ReportException(ex);
  31. }
  32. }
Last edited by AlanJay; Nov 10th, 2010 at 2:29 pm. Reason: Bad indentation
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AlanJay is offline Offline
1 posts
since Nov 2010

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: SQL query: foreach row?
Next Thread in C# Forum Timeline: 3D Graphics





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


Follow us on Twitter


© 2011 DaniWeb® LLC