Ok I added a private member variable:
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 !