Exemplo n.º 1
0
        // Event - Add Address - Click - Adds IP address to collection
        private void UI_btn_AddAddress_Click(object sender, EventArgs e)
        {
            // Checking if textbox is emtpy or in placeholder state
            if (UI_tb_IPAddress.Text.Length == 0 || UI_tb_IPAddress.ForeColor == Color.Gray)
            {
                return;
            }

            // Add IP/user to collection
            AddUser(UI_tb_IPAddress.Text, UI_tb_IPAddress.Text);

            // Update IP Address listbox
            UpdateAddressListBox();

            UI_tb_IPAddress.Clear();
            Textbox_Leave(UI_tb_IPAddress, new EventArgs());
            UI_tb_MessageOut.Focus();
        }
Exemplo n.º 2
0
        // Event - Send Message - Click - Asynchronously send message to all the users in collection
        async private void UI_btn_SendMessage_Click(object sender, EventArgs e)
        {
            // Checking if textbox is emtpy or in placeholder state
            if (UI_tb_MessageOut.Text.Length == 0 || UI_tb_MessageOut.ForeColor == Color.Gray)
            {
                return;
            }

            // Grabbing input
            string username = "";
            string message  = UI_tb_MessageOut.Text;

            // Check if a username is to be embedded in message
            if (UI_tb_Username.Text.Length != 0 && UI_tb_Username.ForeColor != Color.Gray)
            {
                username = $"<{UI_tb_Username.Text}> ";
            }

            // Converting package into byte array to send out
            byte[] messageOut = Encoding.Unicode.GetBytes(username + message);

            // Asynchronously send message to all the users in collection
            await Task.Run(() =>
            {
                // Itterate through user collection and grab IP addresses
                (from i in users select new { i.Key }).ToList().ForEach(x =>
                {
                    try
                    {
                        // Sending message to isolated IP address
                        Task <int> send = clientSocket.SendAsync(messageOut, messageOut.Length, x.Key, 1666);
                        Console.WriteLine($"Bytes Sent: {send.Result}");
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine($"Invalid IP address at {users[x.Key]} : " + exception.Message.ToString());
                    }
                });
            });

            // After sucessful send, clear message box and regain focus
            UI_tb_MessageOut.Clear();
            UI_tb_MessageOut.Focus();
        }