//on server on button private void ButtonServerOn_Click(object sender, RoutedEventArgs e) { //try to parse values and create server try { IPAddress ip = IPAddress.Parse(TBserverIP.Text); int port = Int32.Parse(TBserverPort.Text); _server = new Server(ip, port); } catch//if error with arguments { LbResult.Content = "Invalid ip or port"; return; } try//try run new thread with server { _serverThread = new Thread(_server.StartWorking); _serverThread.Start(); ButtonServerOn.IsEnabled = false; ButtonServerOff.IsEnabled = true; } catch//inform user in bad case { LbResult.Content = "Error with server running"; _server.StopServer(); return; } //create new database worker and save session begin DatabaseWorker dw = new DatabaseWorker(); dw.SaveSession(); }
//ending server work private void EndServerWork() { try { //stop listening if (_server != null) { _server.StopServer(); //save session end to database DatabaseWorker dw = new DatabaseWorker(); dw.SaveSessionEnd(); } } catch { //inform user in bad case LbResult.Content = "Error with finish"; } }
public Client(TcpClient client) { //create new list if he is empty if (_clientList == null) { _clientList = new List <TcpClient>(); } //add our client _clientList.Add(client); //create new dataBase worker DatabaseWorker dw = new DatabaseWorker(); while (true) { //wait message from client string message = WaitMessage(client); //if we recieved usual message with text if (ShowRecieveOperation(message) == "msg:") { string text; string user; //parse text and user name ParseMessage(message, out text, out user); //save to database dw.SaveMessage(text, user); //send message to all users SendToAll(message.Substring(_codeLength, message.Length - _codeLength)); } //if our recieved message is message with authentication else if (ShowRecieveOperation(message) == "ath:") { string userName; string password; //parse user name and password ParseAuthenticationMessage(message, out userName, out password); //if they are not empty if (userName != "" && password != "") { //check password and user name bool result = dw.CheckUserPassword(userName, password); if (result) { //if true sent "true" to this client SendToOne("true", client); //adding ta database dw.AddAuthentication(userName); } //send "false" in all other cases else { SendToOne("false", client); } } else { SendToOne("false", client); } } //if we recieved message with registration else if (ShowRecieveOperation(message) == "reg:") { string userName; string password; string userSurname; string userPassportName; string userEmail; string userCity; //parse this message ParseRegistrationString(message, out userName, out password, out userSurname, out userPassportName, out userEmail, out userCity); //check if such user name is not exist if (dw.CheckUser(userName)) { SendToOne("false", client); } //else send "true" to this client else { SendToOne("true", client); //add to database dw.AddUser(userName, password, userSurname, userPassportName, userEmail, userCity); } } } }
//on show authentications button click private void ButtonShowAuthentication_Click(object sender, RoutedEventArgs e) { DatabaseWorker dw = new DatabaseWorker(); databaseInfo.ItemsSource = dw.GetAuthentications(); }
//on show users button click private void ButtonShowUsers_Click(object sender, RoutedEventArgs e) { DatabaseWorker dw = new DatabaseWorker(); databaseInfo.ItemsSource = dw.GetUsers(); }