// Occures when a new client is accepted private void AcceptClient() { srReceiver = new System.IO.StreamReader(tcpClient.GetStream()); swSender = new System.IO.StreamWriter(tcpClient.GetStream()); // Read the account information from the client currUser = srReceiver.ReadLine(); // We got a response from the client if (currUser != "") { // Store the user name in the hash table if (ChatServer.htUsers.Contains(currUser) == true) { // 0 means not connected swSender.WriteLine("0\"This username already exists."); swSender.Flush(); CloseConnection(); return; } else if (currUser == "Administrator") { // 0 means not connected swSender.WriteLine("0\"This username is reserved."); swSender.Flush(); CloseConnection(); return; } else if (currUser.IndexOf("\"") >= 0) { // 0 means not connected swSender.WriteLine("0\"The character \" is not allowed."); swSender.Flush(); CloseConnection(); return; } else { // 1 means connected successfully swSender.WriteLine("1"); swSender.Flush(); // Add the user to the hash tables and start listening for messages from him ChatServer.AddUser(tcpClient, currUser); } } else { CloseConnection(); return; } try { Thread.Sleep(2000); String[] allUsers = new String[ChatServer.htConnections.Count]; ChatServer.htConnections.Values.CopyTo(allUsers, 0); string addUserRequest = "\"2"; for (int i = 0; i < allUsers.Length; i++) { // Try sending a message to each try { // If the message is blank or the connection is null, break out if (tcpClient == null || allUsers[i] == currUser) { continue; } // Send the message to the current user in the loop addUserRequest += "\"" + allUsers[i]; } catch // If there was a problem, the user is not there anymore, remove him { ChatServer.RemoveUser(tcpClient); } } swSender.WriteLine(addUserRequest); swSender.Flush(); // Keep waiting for a message from the user while (ChatServer.ServRunning && tcpClient.Connected) { if (tcpClient.Available > 0) { strResponse = srReceiver.ReadLine(); // If it's invalid, remove the user if (strResponse == null) { ChatServer.RemoveUser(tcpClient); } else { // Otherwise send the message to all the other users String[] tokens = strResponse.Split('\"'); switch (tokens[0]) { case "": switch (tokens[1]) { case "1": ChatServer.SendBroadcastMessage("\"1\"" + currUser + ": " + tokens[2]); break; case "3": ChatServer.RemoveUser(tcpClient); return; } break; default: switch (tokens[1]) { case "1": ChatServer.SendPrivateMessage(currUser, tokens[0], currUser + "\"1\"" + tokens[2]); break; case "4": ChatServer.SendPrivateMessage(currUser, tokens[0], currUser + "\"4\"" + tokens[2] + "\"" + tokens[3] + "\"" + tokens[4] + "\"" + tokens[5] + "\"" + tokens[6]); break; case "5": ChatServer.SendPrivateMessage(currUser, tokens[0], currUser + "\"5\"" + tokens[2] + "\"" + tokens[3] + "\"" + tokens[4]); break; case "7": ChatServer.SendPrivateMessage(currUser, tokens[0], currUser + "\"7"); break; case "o": ChatServer.SendPrivateMessage(currUser, tokens[0], currUser + "\"o\"" + tokens[2] + "\"" + tokens[3] + "\"" + tokens[4] + "\"" + tokens[5]); break; case "O": ChatServer.SendPrivateMessage(currUser, tokens[0], currUser + "\"O\"" + tokens[2] + "\"" + tokens[3] + "\"" + tokens[4]); break; } break; } } } else { Thread.Sleep(50); } } } catch { // If anything went wrong with this user, disconnect him } ChatServer.RemoveUser(tcpClient); try { tcpClient.Close(); } catch { } }