Stopping a listening thread

  • Thread starter Anders Eriksson
  • Start date
A

Anders Eriksson

I have this code, which I pretty much copied from the net.

My problem is that I cant stop the loop in ListenForClients.
I set Stop=true; but nothing happens...

Please help!

// Anders
--
in my form1.cs

TCPServer server = null;

In a menu handler
private void startToolStripMenuItem_Click(object sender, EventArgs e)
{
server = new
}

private void stopToolStripMenuItem_Click(object sender, EventArgs e)
{
server.Stop = true;
}

class TCPServer
{
public volatile bool Stop = false;
private TcpListener tcpListener;
private Thread listenThread;
private TcpClient tcpClient;
private int port;

public TCPServer()
{
port = Properties.Settings.Default.port;
this.tcpListener = new TcpListener(IPAddress.Any, port);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}

private void ListenForClients()
{
try
{
this.tcpListener.Start();
}
catch (SocketException e)
{
TRACE(e.Message);
throw;
}

while (!Stop)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();

//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new
ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
// close listener
this.tcpListener.Stop();
}

....
}
 
A

Anders Eriksson

Sure. Your loop is blocked at the AcceptTcpClient() call. It won't see
the flag set to true until it accepts a new client. The thread isn't
going to magically continue through the loop just because you've set the
flag.

Hmm... You mean read the comments and you will be enlighted...
Obviously, you have to call Stop() from a thread other than the one
that's called AcceptTcpClient(). Your GUI thread, for example. The docs
suggest that TcpListener instance members are not thread-safe, but it
seems to me that should be a safe method to call cross-thread. If you
run into trouble with that, then disposing the Socket instance returned
from the Server property may have the same effect.

I did just that and it worked perfectly!
If you think that seems too hacky, then you can use the connection

Hacky is my middle name ;-)

Thank you Pete for this help and for all the help you give on this
newsgroup and else where.

// Anders
 
M

Moiv

I have this code, which I pretty much copied from the net.

My problem is that I cant stop the loop in ListenForClients.
I set Stop=true; but nothing happens...

Please help!

// Anders

Forgive me if this is a very basic question as I am new to c# and very
new to network programming.

This seems like a much simpler approach to a multithreaded TCP server
than using the Socket object & the Async methods of BeginAccept &
BeginReceive which I am trying to use in my question posted just before.

What would the downside be (if any) of using the TCPListener object in
this way rather than the Socket object with its Async methods?

I can see that the TCPListener approach does block at a certain point,
which the Socket object doesnt.. but the TCPListener object has been
moved to its own thread by that time.

Regards,
Moiv
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top