示例#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);
     }
 }
        private void btnSendFile_Click_1(object sender, EventArgs e)
        {
            try
            {
                string         fileString = string.Empty;
                OpenFileDialog ofd        = new OpenFileDialog();
                ofd.AutoUpgradeEnabled = false;
                ofd.Filter             = "Text File|*.txt";
                // ofd.Filter = "Image Files |*.JPG;*.PNG| Text File|*.txt";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string path     = ofd.FileName;
                    byte[] fileByte = File.ReadAllBytes(path);
                    fileString += Encoding.ASCII.GetString(fileByte);

                    MessagePackage package = new MessagePackage();
                    string         toSendMessage;
                    toSendMessage = package.ConcatenateMessage(MessageType, 1, MyName, this.Text, fileString);
                    byte[] buff = Encoding.ASCII.GetBytes(toSendMessage);
                    ClientSocket.BeginSend(buff, 0, buff.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
                }
            }
            catch (SocketException) { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
示例#3
0
        private void btnChatRoom_Click(object sender, EventArgs e)
        {
            List <string> partnerList = new List <string>();

            foreach (string name in UsernameList)
            {
                if (name.Equals(myName))
                {
                    continue;
                }
                partnerList.Add(name);
            }
            SelectChatRoom select = new SelectChatRoom();

            select.UserNameList = partnerList;
            select.ParentScreen = this;
            select.ShowDialog();

            MessagePackage mp = new MessagePackage();

            chatRoomParticipants.Add(myName);
            string partnerString = mp.ToUsernameString(chatRoomParticipants);

            if (openWindows.ContainsKey(partnerString) == false)
            {
                MessagingScreen messagingScreen = OpenNewMessagingScreen(chatRoomParticipants, partnerString, 2);
                openWindows.Add(partnerString, messagingScreen);
                messagingScreen.Show();
            }
        }
 private void buttonSend_Click(object sender, EventArgs e)
 {
     btnSendFile.Enabled = true;
     try
     {
         MessagePackage package = new MessagePackage();
         string         toSendMessage;
         toSendMessage = package.ConcatenateMessage(MessageType, 0, MyName, this.Text, textBoxType.Text);
         byte[] buff = Encoding.ASCII.GetBytes(toSendMessage);
         ClientSocket.BeginSend(buff, 0, buff.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
     }
     catch (SocketException) { }
     catch (Exception ex)
     {
         MessageBox.Show("2 " + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
示例#5
0
        private void buttonConnect_Click_1(object sender, EventArgs e)
        {
            //I am trying to connect tto the server
            MessageBox.Show("clicked On connect Button");
            try
            {
                ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ClientSocket.BeginConnect(new IPEndPoint(IPAddress.Parse(this.textBoxAddress.Text), Int32.Parse(this.textBoxPort.Text)), new AsyncCallback(ConnectCallback), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            try
            {
                MessagePackage package     = new MessagePackage();
                string         fullMessage = package.ConcatenateMessage(4, 0, textBoxName.Text, String.Empty, String.Empty);
                buffer = Encoding.ASCII.GetBytes(fullMessage);
                ClientSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
            }
            catch (SocketException) { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            try
            {
                buffer = new byte[ClientSocket.ReceiveBufferSize];
                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);
            }
        }
示例#6
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);
            }
        }