Example #1
0
 private void ReceiveCallback(IAsyncResult AR)
 {
     try
     {
         int            received = ClientSocket.EndReceive(AR);
         string         text     = Encoding.ASCII.GetString(buffer);
         MessagePackage package  = new MessagePackage();
         Tuple <int, int, string, string, string> tuple = package.SliceMessage(text.Substring(0, received));
         if (tuple.Item1 == 0)
         {
             LoggedInScreen loggedinScreen = new LoggedInScreen(ClientSocket, textBoxName.Text);
             loggedinScreen.UsernameList = package.ToUsernameList(tuple.Item5);
             this.Hide();
             loggedinScreen.ShowDialog();
         }
         else if (tuple.Item1 == 5)
         {
             MessageBox.Show("This username already exists!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
             return;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Example #2
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                int            received = ClientSocket.EndReceive(ar);
                string         text     = Encoding.ASCII.GetString(buffer);
                MessagePackage package  = new MessagePackage();
                Tuple <int, int, string, string, string> tuple = package.SliceMessage(text.Substring(0, received));
                //uzenet tipusa
                switch (tuple.Item1)
                {
                case 0:
                    UsernameList = package.ToUsernameList(tuple.Item5);
                    listBoxContactList.Items.Clear();
                    foreach (string name in UsernameList)
                    {
                        if (name.Equals(myName))
                        {
                            continue;
                        }
                        listBoxContactList.Items.Add(name);
                    }
                    this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                    break;

                //privat uzenet
                case 1:
                    if (tuple.Item2 == 0)     //nincs fajl - 0
                    {
                        if (tuple.Item3.Equals(myName))
                        {
                            MessagingScreen megnyitott;
                            megnyitott = openWindows[tuple.Item4];
                            megnyitott.AppendToTextBox(tuple.Item5, tuple.Item3);
                            this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                        }
                        else if (!tuple.Item3.Equals(myName))
                        {
                            if (openWindows.ContainsKey(tuple.Item3) == true)
                            {
                                MessagingScreen megnyitott;
                                megnyitott = openWindows[tuple.Item3];
                                megnyitott.AppendToTextBox(tuple.Item5, tuple.Item3);
                                this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                            }
                            else
                            {
                                MessagingScreen messagingScreen = OpenNewMessagingScreen(new List <string>(), tuple.Item3, 1);
                                messagingScreen.AppendToTextBox(tuple.Item5, tuple.Item3);
                                openWindows.Add(tuple.Item3, messagingScreen);
                                messagingScreen.Show();
                                this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                                Application.Run();
                            }
                        }
                    }
                    else if (tuple.Item2 == 1)    // van fajl kuldes
                    {
                        if (tuple.Item3.Equals(myName))
                        {
                            MessagingScreen megnyitott;
                            megnyitott = openWindows[tuple.Item4];
                            megnyitott.AppendToTextBox("File sent", tuple.Item3);
                            this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                        }
                        else
                        {
                            try
                            {
                                byte[]         fileByte = Encoding.ASCII.GetBytes(tuple.Item5);
                                SaveFileDialog sfd      = new SaveFileDialog();
                                sfd.AutoUpgradeEnabled = false;
                                sfd.Filter             = "Text File|*.txt";
                                //sfd.Filter = "Image Files |*.JPG;*.PNG| Text File|*.txt";
                                if (sfd.ShowDialog() == DialogResult.OK)
                                {
                                    File.WriteAllBytes(sfd.FileName, Encoding.ASCII.GetBytes(tuple.Item5));
                                }
                                if (openWindows.ContainsKey(tuple.Item3) == true)
                                {
                                    MessagingScreen megnyitott;
                                    megnyitott = openWindows[tuple.Item3];
                                    megnyitott.AppendToTextBox("File received", tuple.Item3);
                                    this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    break;

                //chatsszoba
                case 2:
                    if (tuple.Item2 == 0)     //nincs fajlkuldes
                    {
                        if (openWindows.ContainsKey(tuple.Item4) == true)
                        {
                            MessagingScreen megnyitott;
                            megnyitott = openWindows[tuple.Item4];
                            megnyitott.AppendToTextBox(tuple.Item5, tuple.Item3);
                            this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                        }
                        else
                        {
                            MessagingScreen messagingScreen = OpenNewMessagingScreen(chatRoomParticipants, tuple.Item4, 2);
                            messagingScreen.AppendToTextBox(tuple.Item5, tuple.Item3);
                            openWindows.Add(tuple.Item4, messagingScreen);
                            messagingScreen.Show();
                            this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                            Application.Run();
                        }
                    }
                    else if (tuple.Item2 == 1)     // van fajlkuldes
                    {
                        if (tuple.Item3.Equals(myName))
                        {
                            MessagingScreen megnyitott;
                            megnyitott = openWindows[tuple.Item4];
                            megnyitott.AppendToTextBox("File sent", tuple.Item3);
                            this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                        }
                        else
                        {
                            try
                            {
                                byte[]         fileByte = Encoding.ASCII.GetBytes(tuple.Item5);
                                SaveFileDialog sfd      = new SaveFileDialog();
                                sfd.AutoUpgradeEnabled = false;
                                sfd.Filter             = "Text File|*.txt";
                                //sfd.Filter = "Image Files | *.BMP; *.JPG; *.GIF | All files(*.*) | *.*";
                                if (sfd.ShowDialog() == DialogResult.OK)
                                {
                                    File.WriteAllBytes(sfd.FileName, Encoding.ASCII.GetBytes(tuple.Item5));
                                }
                                if (openWindows.ContainsKey(tuple.Item4) == true)
                                {
                                    MessagingScreen megnyitott;
                                    megnyitott = openWindows[tuple.Item4];
                                    megnyitott.AppendToTextBox("File received", tuple.Item3);
                                    this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    break;

                //mindenkinek kuldes
                case 3:
                    //ha nincs fajl
                    if (tuple.Item2 == 0)
                    {
                        if (openWindows.ContainsKey(tuple.Item4) == true)
                        {
                            MessagingScreen megnyitott;
                            megnyitott = openWindows[tuple.Item4];
                            megnyitott.AppendToTextBox(tuple.Item5, tuple.Item3);
                            this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                        }
                        else
                        {
                            MessagingScreen messagingScreen = OpenNewMessagingScreen(UsernameList, tuple.Item4, 3);
                            messagingScreen.AppendToTextBox(tuple.Item5, tuple.Item3);
                            openWindows.Add(tuple.Item4, messagingScreen);
                            messagingScreen.Show();
                            this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                            Application.Run();
                        }
                    }
                    else if (tuple.Item2 == 1)
                    {
                        if (tuple.Item3.Equals(myName))
                        {
                            MessagingScreen megnyitott;
                            megnyitott = openWindows[tuple.Item4];
                            megnyitott.AppendToTextBox("File sent", tuple.Item3);
                            this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                        }
                        else
                        {
                            try
                            {
                                byte[]         fileByte = Encoding.ASCII.GetBytes(tuple.Item5);
                                SaveFileDialog sfd      = new SaveFileDialog();
                                sfd.AutoUpgradeEnabled = false;
                                sfd.Filter             = "Text File|*.txt";
                                if (sfd.ShowDialog() == DialogResult.OK)
                                {
                                    File.WriteAllBytes(sfd.FileName, Encoding.ASCII.GetBytes(tuple.Item5));
                                }
                                if (openWindows.ContainsKey(tuple.Item4) == true)
                                {
                                    MessagingScreen megnyitott;
                                    megnyitott = openWindows[tuple.Item4];
                                    megnyitott.AppendToTextBox("File received", tuple.Item3);
                                    this.ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }