Windows service and threads and sockets

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

Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Windows service and threads and sockets

 
0
  #1
Apr 2nd, 2006
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?
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Windows service and threads and sockets

 
0
  #2
Apr 2nd, 2006
Ok I added a private member variable:

  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:

  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:

  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 !
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 233
Reputation: Lord Soth is an unknown quantity at this point 
Solved Threads: 4
Lord Soth's Avatar
Lord Soth Lord Soth is offline Offline
Posting Whiz in Training

Re: Windows service and threads and sockets

 
0
  #3
Apr 5th, 2006
Hi,

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

Loren Soth
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 1
Reputation: masihyeganeh is an unknown quantity at this point 
Solved Threads: 0
masihyeganeh masihyeganeh is offline Offline
Newbie Poster

Re: Windows service and threads and sockets

 
0
  #4
Jun 24th, 2008
Originally Posted by Lord Soth View Post
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 23
Reputation: $dunk$ is an unknown quantity at this point 
Solved Threads: 5
$dunk$ $dunk$ is offline Offline
Newbie Poster

Re: Windows service and threads and sockets

 
0
  #5
Jul 7th, 2008
You can always call Abort on the thread.

  1. m_thread.Abort();

If you need to do any cleanup, put a try-catch around your while loop.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC