class Server
{
private volatile bool _isStopRequested=false;
private TcpListener tcpListener;
private Thread listenThread;
public Server()
{
this.tcpListener = new TcpListener(IPAddress.Any, 3000);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
}
public void Start()
{
this.listenThread.Start();
}
public void Stop(){
_isStopRequested=false;
}
private void ListenForClients()
{
this.tcpListener.Start();
while (!_isStopRequested)
{
TcpClient client = this.tcpListener.AcceptTcpClient();
}
}
}
i think you will catch the basic idea of starting and stopping service.