Group Chat Example



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace MulticastChat
{
    public partial class Form1 : Form
    {
        Socket cs;
        Thread t;
        IPEndPoint gip;
        public Form1()
        {
            InitializeComponent();
            gip = new IPEndPoint(IPAddress.Parse(GroupIptextBox.Text), 7080);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                panel1.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void Receive()
        {
            try
            {
                EndPoint ep = (EndPoint)gip;
                byte[] data = new byte[2048];
                string Msg;
                int recv;
                while (true)
                {
                    recv = cs.ReceiveFrom(data, ref ep);
                    Msg = Encoding.ASCII.GetString(data, 0, recv);
                    string UserName = "";
                    UserName = Msg.Substring(0, Msg.IndexOf(":"));
                    Msg = Msg.Substring(Msg.IndexOf(":") + 1);
                    ReceverichTextBox.Text = ReceverichTextBox.Text + UserName + " : " + Msg + "\n";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Sendbutton_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] msg = Encoding.ASCII.GetBytes(UserNametextBox.Text + " : " + SendtextBox.Text);
                SendtextBox.Clear();
                cs.SendTo(msg, SocketFlags.None, gip);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Connectbutton_Click(object sender, EventArgs e)
        {
            try
            {
                if (GroupIptextBox.Text != "")
                {
                    if (UserNametextBox.Text != "")
                    {
                        if (Connectbutton.Text == "Connect")
                        {
                            cs = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                            IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 7080);
                            cs.Bind(ipe);
                            cs.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.10.0.1")));
                            t = new Thread(new ThreadStart(Receive));
                            t.IsBackground = true;
                            t.Start();
                            panel1.Enabled = true;
                            Connectbutton.Text = "Disconnect";
                            GroupIptextBox.ReadOnly = true;
                            UserNametextBox.ReadOnly = true;
                        }
                        else if (Connectbutton.Text == "Disconnect")
                        {
                            t.Abort();
                            cs.Close();
                            GroupIptextBox.ReadOnly = false;
                            UserNametextBox.ReadOnly = false;
                            panel1.Enabled = false;
                            Connectbutton.Text = "Connect";
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please Enter Your Name...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("Please Enter Group Ip...", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

No comments: