TcpListener how to abort?

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...
 
N

Neno Loje [MVP C#]

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
 
C

Coder

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..
 

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