Exemplo n.º 1
0
        private void btnRegister_Click(object sender, RoutedEventArgs e)
        {
            SqlConnection sqlConnection = DBService.Get_DB_Connection();
            string        QUERY         = "SELECT COUNT(*) FROM db_Clients WHERE Username=@Username1";
            SqlCommand    sqlComm       = new SqlCommand(QUERY, sqlConnection);

            sqlComm.CommandType = System.Data.CommandType.Text;
            sqlComm.Parameters.AddWithValue("@Username1", txtUsername.Text);
            int count = Convert.ToInt32(sqlComm.ExecuteScalar());

            if (count == 0)
            {
                //Then we can register new client
                QUERY   = "INSERT INTO db_Clients (Username, Password, IsLogged) VALUES (@Username2, @Password2, 0)";
                sqlComm = new SqlCommand(QUERY, sqlConnection);
                sqlComm.Parameters.AddWithValue("@Username2", txtUsername.Text);
                sqlComm.Parameters.AddWithValue("@Password2", txtPassword.Password);
                sqlComm.ExecuteNonQuery();
                MessageBox.Show("Registration successful!");
            }
            else
            {
                MessageBox.Show("This username is already taken.");
            }

            DBService.Close_DB_Connection();
        }
Exemplo n.º 2
0
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            SqlConnection sqlConnection = DBService.Get_DB_Connection();
            string        QUERY         = "SELECT COUNT(*) FROM db_Clients WHERE Username=@Username1 AND Password=@Password1 AND IsLogged <> 1";
            SqlCommand    sqlComm       = new SqlCommand(QUERY, sqlConnection);

            sqlComm.CommandType = System.Data.CommandType.Text;
            sqlComm.Parameters.AddWithValue("@Username1", txtUsername.Text);
            sqlComm.Parameters.AddWithValue("@Password1", txtPassword.Password);
            int count = Convert.ToInt32(sqlComm.ExecuteScalar());

            if (count == 1)
            {
                //Update the IsLogged to 1
                QUERY   = "UPDATE db_Clients SET IsLogged=1 WHERE Username=@Username2 AND Password=@Password2";
                sqlComm = new SqlCommand(QUERY, sqlConnection);
                sqlComm.Parameters.AddWithValue("@Username2", txtUsername.Text);
                sqlComm.Parameters.AddWithValue("@Password2", txtPassword.Password);
                sqlComm.ExecuteNonQuery();
                ClientWindow clientWindow = new ClientWindow(txtUsername.Text);
                clientWindow.Show();
                this.Close();
            }
            else
            {
                //The username or password was incorrect or the user is already logged in.
                MessageBox.Show("Username or Password is incorrect or the user is already logged in.");
            }

            //Close connection to DB
            DBService.Close_DB_Connection();
        }
Exemplo n.º 3
0
        private void Window_Closing(object sender, EventArgs e)
        {
            //Change the IsLogged to 0
            //Send disconnect to server with username
            Console.WriteLine("Window_Closing was called.");
            SqlConnection sqlConnection = DBService.Get_DB_Connection();
            string        QUERY         = "UPDATE db_Clients SET IsLogged=0 WHERE Username=@Username";
            SqlCommand    sqlComm       = new SqlCommand(QUERY, sqlConnection);

            sqlComm.Parameters.AddWithValue("@Username", user.username);
            sqlComm.ExecuteNonQuery();
            DBService.Close_DB_Connection();

            client.Send($"disconnect|{user.username}");
        }
Exemplo n.º 4
0
        private void Events_DataReceived(object sender, DataReceivedEventArgs e)
        {
            string[] message = Encoding.UTF8.GetString(e.Data).Split('|');

            if (message[0].Equals("file"))
            {
                //Implement broadcast or mutlicast, not really needed though but anyway
                Console.WriteLine(message[4]);

                SaveFileDialog dialog = new SaveFileDialog
                {
                    Filter           = "All files(*.*)|*.*",
                    RestoreDirectory = true,
                    Title            = "Where do you want to save the file?",
                    FileName         = ""
                };
                var extension = message[2];

                var fileName = string.Empty;
                if (dialog.ShowDialog() == true)
                {
                    fileName = dialog.FileName;
                }

                if (fileName != string.Empty)
                {
                    if (!fileName.Contains(extension))
                    {
                        fileName += extension;
                    }

                    File.WriteAllText(fileName, message[4]);

                    txtInfo.Dispatcher.Invoke((Action) delegate
                    {
                        txtInfo.Text += $"{message[3]} sent a file, saved to: {fileName}{Environment.NewLine}";
                    });
                }
                else
                {
                    txtInfo.Dispatcher.Invoke((Action) delegate
                    {
                        txtInfo.Text += $"{message[3]} sent a file, it didn't work{Environment.NewLine}";
                    });
                }
            }
            else if (message[0].Equals("disconnected"))
            {
                lstClientIP.Dispatcher.Invoke((Action) delegate()
                {
                    lstClientIP.Items.Remove(message[1]);
                });
            }
            else if (message[0].Equals("connected"))
            {
                lstClientIP.Dispatcher.Invoke((Action) delegate()
                {
                    lstClientIP.Items.Add(message[1]);
                });
            }
            else if (message[0].Equals("multicast"))
            {
                txtInfo.Dispatcher.Invoke((Action) delegate()
                {
                    txtInfo.Text += $"{message[1]}: {message[2]}{Environment.NewLine}";
                });
            }
            else if (message[0].Equals("broadcast"))
            {
                txtInfo.Dispatcher.Invoke((Action) delegate()
                {
                    txtInfo.Text += $"{message[1]} to Everyone: {message[2]}{Environment.NewLine}";
                });
            }
            else if (message[0].Equals("listofconnections"))
            {
                //Updating the IpPort for this.user and user in DB.
                user.ipPort = message[2];
                SqlConnection sqlConnection = DBService.Get_DB_Connection();
                string        QUERY         = "UPDATE db_Clients SET IpPort=@IpPort WHERE Username=@Username2";
                SqlCommand    sqlComm       = new SqlCommand(QUERY, sqlConnection);
                sqlComm.CommandType = System.Data.CommandType.Text;
                sqlComm.Parameters.AddWithValue("@IpPort", user.ipPort);
                sqlComm.Parameters.AddWithValue("@Username2", message[1]);
                sqlComm.ExecuteNonQuery();
                DBService.Close_DB_Connection();

                int           NoOfConnections   = int.Parse(message[3]);
                List <string> listOfConnections = new List <string>();
                lstClientIP.Dispatcher.Invoke((Action) delegate()
                {
                    for (int i = 0; i < NoOfConnections; i++)
                    {
                        listOfConnections.Add(message[i + 4]);
                        lstClientIP.Items.Add(message[i + 4]);
                    }
                });
            }
        }