TcpListener how to abort?

  • Thread starter Thread starter Coder
  • Start date Start date
C

Coder

hi, i have a simple question; Suppose that followig code is a thread;

/***************************************************/
listener = new TcpListener( IP, PORT);

listener.Start();


while(running) {

sock = listener.AcceptSocket(); // this is blocking... suppose no client
connects

/*bla bla bla*/

}

/***************************************************/

How can i release(dipose, free) the listener socket. i have tried
"listener.Stop()" , aborting thread etc....

i just want to break it even by creating exception or by normal way(i don
kno how).

Best regards...
 
Hi Coder, (<-- would use your full name)

call the stop method to close the TcpListener to stop listening (and free
resources). The Stop method does not close any accepted connections. You are
responsible for closing these separately. You can do that by calling the
Close on the TcpClient.

<code>
listener.Start();

while(running)
{

TcpClient client = listener.AcceptTcpClient();
// do some work with the TcpClient here

client.Close();
}

listener.Stop();
</code>

Cheers,
Neno
 
listener.Start();

while(running)
{

TcpClient client = listener.AcceptTcpClient();
// do some work with the TcpClient here

client.Close();
}

//-----suppose that above code is running in a thread

i use
listener.Stop();
while it is still in listenning mode. I closed all the accepted sockets. But
altough i stop listener (listener.Stop() ) ,thread still hangs... (i see the
processes running on the tast manager).

1 year ago i wrote an mutlithreaded application like this one, may now im
forgetting something.

10x for the answer, Cheers..
 
Back
Top