954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Windows service and threads and sockets

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?

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

Ok I added a private member variable:

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:

while(RUNNING){
	Socket s = this.l.AcceptSocket();
				
	Char[] carr = quotes[numServed%2].ToCharArray();
	Byte[] barr = Encoding.ASCII.GetBytes(carr);
				
	s.Send(barr, barr.Length, 0);
	s.Shutdown(SocketShutdown.Both);
	s.Close();
				
	numServed++;
}


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:

protected override void OnStop()
		{
			// TODO: Add tear-down code here (if required) to stop your service.
			
			this.RUNNING = false;
			
			//Make a connection to force an iteration in method qotdSocketOpen()
			Socket socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			IPEndPoint remote = new IPEndPoint(ip, port);
			socClient.Connect(remote);
			socClient.Shutdown(SocketShutdown.Both);
			socClient.Close();
		
		}


Problem Solved !

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

Hi,

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

Loren Soth

Lord Soth
Posting Whiz in Training
233 posts since Mar 2006
Reputation Points: 28
Solved Threads: 4
 

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

masihyeganeh
Newbie Poster
1 post since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

You can always call Abort on the thread.

m_thread.Abort();


If you need to do any cleanup, put a try-catch around your while loop.

$dunk$
Newbie Poster
23 posts since Mar 2008
Reputation Points: 14
Solved Threads: 5
 

You can always use a polling methodology:

private void TcpListen()
        {
            try
            {
                LogMan.Report("Starting Listener on port {0}.", SettingsManager.ListenerPort);

                TcpListener listener = new TcpListener(IPAddress.Any, SettingsManager.ListenerPort);
                listener.Start();

                while (true)
                {
                    if (_stop)
                        break;

                    if (!_pause)
                    {
                        if (listener.Pending())
                        {
                            TcpClient client = listener.AcceptTcpClient();
                            LogMan.Report("Client Connected to Listener.");
                            Thread thread = new Thread(new ParameterizedThreadStart(HandleClientConnection));
                            thread.Start(client);
                        }
                    }
                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                LogMan.ReportException(ex);
            }
        }
AlanJay
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You