/// <summary> /// Called from within ChatDialogForm to disconnect /// the client and remove them from TreeView. /// </summary> /// <param name="remoteIP"></param> /// <param name="remotePort"></param> public void DisconnectClient(string remoteIP, string remotePort) { Socket selectedSocket; //Delete the client from Tree View UpdateTreeView(chatForm.connectedStudent.StudentFirstName + " " + chatForm.connectedStudent.StudentLastName, "Delete"); //Find client ChatDialog box corresponding to this socket int counter = 0; foreach (ChatDialogForm c in formList) { if (c.remoteEndPoint.Equals(remoteIP) && c.remotePort.Equals(remotePort)) { selectedSocket = c.connectedClient.Client; selectedSocket.Close(); break; } counter++; } //Terminate ChatDialog box ChatDialogForm cd = (ChatDialogForm)formList[counter]; formList.RemoveAt(counter); ((Thread)(threadList[counter])).Abort(); threadList.RemoveAt(counter); }
/// <summary> /// Event: fired when a client gets connected. Performs following actions: /// 1. Update tree view /// 2. Open a chat box to chat with client /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ClientAdded(object sender, EventArgs e) { tcpClient = ((MyEventArgs)e).clientSock; //Show dialog box to facilitate chat chatForm = new ChatDialogForm(this, tcpClient, staffMember); //Add dialog box object to formList //Add current thread to threadList //Call delegate function to update tree view for (int i = 0; i < 100; i++) { try { UpdateTreeView(chatForm.connectedStudent.StudentFirstName + " " + chatForm.connectedStudent.StudentLastName, "Add"); break; } catch (System.NullReferenceException) { //Free the processor to do other things Thread.Sleep(100); } catch (Exception ex) { MyMessageBox.ShowMessage("An error occurred: " + ex.ToString()); break; } } formList.Add(chatForm); threadList.Add(Thread.CurrentThread); chatForm.ShowDialog(); }