Example 1:
1. Client A connects to Server
2. Client B connects to Server
3. A disconnects from server causing crash
or this order:
Example 2:
1. Client A connects to Server
2. Client B connects to Server
3. B disconnects from server *no Crash*
4. A disconnects causing crash
A little more information now. When only one client is connected and decides to disconnect, the thread.stop() properly closes the thread for that connection. actually now that I think of it, when example 2 takes place. Client A works perfectly fine till he decides to disconnect, then the server crashes. The crash seems to occur when the thread begins the loop again and tries to read a closed streamreader. Keep in mind that when the disconnect is called it not only calls a stop on the Thread but also shuts down the stream. So the IOException makes perfect sense. What doesn't is that the clients must seemingly disconnect in reverse order to avoid the crash, which is obviously unacceptable. If you spent enough time to read all of that, i thank you. Now comes the code.
Expand|Select|Wrap|Line Numbers
- static void CommandFunction()
- {
- NetworkStream ns = new NetworkStream(client);
- StreamReader sr = new StreamReader(ns);
- StreamWriter sw = new StreamWriter(ns);
- while (true)
- {
- string data;
- IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
- data = "";
- data = sr.ReadLine();
- String message = data;
- switch (message)
- {
- case "server_quit":
- {
- Console.WriteLine("Disconnecting With {0} ", newclient.Address);
- ns.Close();
- sr.Close();
- sw.Close();
- Console.WriteLine("Connection Terminated");
- CommandThread.Abort();
- break;
- }
- }
- }
- }