Sign in to follow this  
SelethD

How exit a TCPListener.Accept thread?

Recommended Posts

Im a bit new to this whole networking, and threading thing. So this may seem simple question, but I have searched google all day and I cant find a clear answer:

This is in Visual C# 2010
I am making a server
I have my main loop create a thread for listening and accepting clients...

[code]
private static void ListenThread()
{
Console.WriteLine("Server started... waiting for clients.");
while (bKeepAlive)
{
TcpClient client = ls.AcceptTcpClient();

cCharacter Character = new cCharacter();
Character.mClient = client;
Console.WriteLine("Client Connected: " + client.Client.RemoteEndPoint.ToString());

if (!gCharacterManager.AddCharacter(Character))
{
Character.Disconnect();
Console.WriteLine("Client disconnected, server is at full capacity.");
}
}
}
[/code]

The problem is... when the main loop needs to shut down the server, how do I properly get out of this tread?
If I set bKeepAlive = false; The thread wont exit since it will be stuck at the ls.AcceptTCPClient(); statment.
So I tried to have the main loop abort the thread with ... [font="Consolas"][size="2"][font="Consolas"][size="2"]tListenThread.Abort(); however this caused the following error...
"A blocking operation was interrupted by a call to WSACancelBlockingCall"
The debugger highlights my "[color="#2b91af"]TcpClient[/color][font="Consolas"][size="2"][font="Consolas"][size="2"] client = ls.AcceptTcpClient();" line as the source of this error
But the error doesnt happen every time I close the server?

So is there a proper way I am supposed to end this thread when the server exits?

Thankyou.


[/size][/font][/size][/font][/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
[quote name='SelethD' timestamp='1307718258' post='4821733']
So I tried to have the main loop abort the thread with ... [font="Consolas"][size="2"][font="Consolas"][size="2"]tListenThread.Abort(); however this caused the following error...
"A blocking operation was interrupted by a call to WSACancelBlockingCall"
The debugger highlights my "[color="#2b91af"]TcpClient[/color][font="Consolas"][size="2"][font="Consolas"][size="2"] client = ls.AcceptTcpClient();" line as the source of this error
But the error doesnt happen every time I close the server?
[font=arial, verdana, tahoma, sans-serif][size=3][/quote][/size][/font]
[/size][/font][/size][/font][/size][/font][/size][/font]
That's not an "error" in the sense that your program is wrong. You should catch that exception, probably outside the while() loop, and treat that as an acceptable shutdown path for that thread.

Or you could create a new TCP connection to yourself, and immediately disconnect -- the whole point is just to break out of the Accept() once you've set the boolean to false.

By the way: make sure the bool for the while look is declared volatile if you'll write it from a different thread!

Share this post


Link to post
Share on other sites
So, you mean something like this?

[code] private static void ListenThread()
{
Console.WriteLine("Server started... waiting for clients.");
try
{
while (true)
{
TcpClient client = ls.AcceptTcpClient();

cCharacter Character = new cCharacter();
Character.mClient = client;
Console.WriteLine("Client Connected: " + client.Client.RemoteEndPoint.ToString());

if (!gCharacterManager.AddCharacter(Character))
{
Character.Disconnect();
Console.WriteLine("Client disconnected, server is at full capacity.");
}
}
}
catch
{
}
}
[/code]

and then just use the Thread.Abort() to signal it to stop?

i have a few more threads that rely on the bool bKeepAlive to determine when to stop running, you had said use volatile....
like this? [font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]

volatile[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]static[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]bool[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] bKeepAlive = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];

thanks so much for the help

[/size][/font][/size][/font]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this