TcpListener.AcceptTcpClient query
-
TcpListener.AcceptTcpClient
is a blocking call. How do I elegantly stop the call? From another thread say I want to stop listening. If I callTcpListener.Stop
on theTcpListener
object I get an exception. Some funny exception about how I halted a blocking operation. Right now I am putting an emptytry{}...catch{}
block and evading this exception. But there must be a more elegant solution! Any help is hugely appreciated! Warm regards Nish
Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]
-
TcpListener.AcceptTcpClient
is a blocking call. How do I elegantly stop the call? From another thread say I want to stop listening. If I callTcpListener.Stop
on theTcpListener
object I get an exception. Some funny exception about how I halted a blocking operation. Right now I am putting an emptytry{}...catch{}
block and evading this exception. But there must be a more elegant solution! Any help is hugely appreciated! Warm regards Nish
Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]
Untested; but give this a shot. Instead of calling
AcceptTcpClient()
from the start; make calls toPending()
instead, and when it returns true THEN make the call toAcceptTcpClient
. Since that probably sounds convoluted, here's some code to illustrate it :)TcpListener tcpL = ....;
tcpL.Start();
while(bShouldAcceptConnections)
{
if( tcpL.Pending() )
{
TcpClient tcpC = tcpL.AcceptTcpClient();
// do something with it
}// Other processing that will eventually set
// bShouldAcceptConnections to false
}tcpL.Stop();
HTH, James "Java is free - and worth every penny." - Christian Graus
-
Untested; but give this a shot. Instead of calling
AcceptTcpClient()
from the start; make calls toPending()
instead, and when it returns true THEN make the call toAcceptTcpClient
. Since that probably sounds convoluted, here's some code to illustrate it :)TcpListener tcpL = ....;
tcpL.Start();
while(bShouldAcceptConnections)
{
if( tcpL.Pending() )
{
TcpClient tcpC = tcpL.AcceptTcpClient();
// do something with it
}// Other processing that will eventually set
// bShouldAcceptConnections to false
}tcpL.Stop();
HTH, James "Java is free - and worth every penny." - Christian Graus
Yes, I have an application using pretty much the same logic and it works ok. Andres Manggini. Buenos Aires - Argentina.