Multicast Network Chat in C#

In my network programming class CS3670 at UVU (UVSC) we had an assignment to write a multicast chat program that would allow everyone in the class talk in the same chat window. This was used to show that we were able to use the multicast addresses and to also demonstrate our use of the UDP client libraries. Here is my solution:

  1 using System;

  2 using System.Collections.Generic;

  3 using System.ComponentModel;

  4 using System.Data;

  5 using System.Drawing;

  6 using System.Text;

  7 using System.Windows.Forms;

  8 using System.Net;

  9 using System.Net.Sockets;

 10 using System.Threading;

 11 

 12 namespace MultiCastChat

 13 {

 14     public partial class formMain : Form

 15     {

 16         private const int TIME_TO_LIVE = 50;

 17         private UdpClient client = null;

 18         private IPAddress group = null;

 19         private int port = 0;

 20         private IPEndPoint multiCastEP = null;

 21 

 22         private bool stayAlive = true;

 23         private Thread receiveThread = null;

 24         private string defaultNameText = "";

 25 

 26         public delegate void dListOngoing(string message);

 27         public dListOngoing listOngoing;

 28 

 29         public formMain()

 30         {

 31             InitializeComponent();

 32         }

 33 

 34         private void formMain_Load(object sender, EventArgs e)

 35         {

 36             comboAddress.SelectedIndex = 0;

 37             defaultNameText = textboxName.Text;

 38             this.AcceptButton = buttonJoin;

 39             this.CancelButton = buttonExit;

 40             this.buttonJoin.Enabled = true;

 41             this.buttonExit.Enabled = true;

 42             this.buttonSend.Enabled = true;

 43             this.textboxPort.Text = "" + 10002;

 44             textboxInput.Focus();

 45             listOngoing = new dListOngoing(displayMessage);

 46         }

 47 

 48         private void buttonSend_Click(object sender, EventArgs e)

 49         {

 50             this.SendMessage(textboxInput.Text);

 51         }

 52 

 53         private void buttonEnd_Click(object sender, EventArgs e)

 54         {

 55             this.LeaveGroup();

 56 

 57             buttonJoin.Enabled = true;

 58             buttonEnd.Enabled = false;

 59             buttonSend.Enabled = false;

 60             this.AcceptButton = buttonJoin;

 61             this.CancelButton = buttonExit;

 62         }

 63 

 64         private void SendMessage(String message)

 65         {

 66             Byte[] buff;

 67 

 68             buff = Encoding.ASCII.GetBytes(textboxName.Text + ": " + message);

 69             client.Send(buff, buff.Length, multiCastEP);

 70             textboxInput.Text = "";

 71         }

 72 

 73         private void LeaveGroup()

 74         {

 75             if (group != null)

 76             {

 77                 SendMessage("Leaving Chat");

 78                 Application.DoEvents();

 79                 Thread.Sleep(500);

 80                 stayAlive = false;

 81                 while (receiveThread.IsAlive) Application.DoEvents();

 82                 receiveThread.Join();

 83                 client.DropMulticastGroup(group);

 84                 client.Close();

 85                 client = null;

 86                 group = null;

 87                 multiCastEP = null;

 88             }

 89         }

 90 

 91         private void JoinGroup()

 92         {

 93             if (!int.TryParse(textboxPort.Text, out port))

 94                 throw new ApplicationException("Invalid Port Number");

 95             if (!IPAddress.TryParse(comboAddress.Text, out group))

 96                 throw new ApplicationException("Invalid Multicast Group Address");

 97 

 98             client = new UdpClient(port);

 99             client.JoinMulticastGroup(group, TIME_TO_LIVE);

100             multiCastEP = new IPEndPoint(group, port);

101 

102             this.stayAlive = true;

103             receiveThread = new Thread(this.runThread);

104             receiveThread.Start();

105 

106             this.SendMessage("has joined the chat!");

107             this.AcceptButton = buttonSend;

108             this.CancelButton = buttonEnd;

109             this.buttonJoin.Enabled = false;

110             this.buttonEnd.Enabled = true;

111             this.buttonSend.Enabled = true;

112             textboxInput.Focus();

113         }

114 

115         private void runThread()

116         {

117             Byte[] buff;

118             String message;

119             while (stayAlive)

120             {

121                 IPEndPoint ep = null;

122                 if (client.Available > 0)

123                 {

124                     buff = client.Receive(ref ep);

125                     message = Encoding.ASCII.GetString(buff);

126                     this.Invoke(this.listOngoing, message);

127                 }

128                 else

129                 {

130                     Thread.Sleep(10);

131                 }

132             }

133         }

134 

135         private void buttonExit_Click(object sender, EventArgs e)

136         {

137             this.LeaveGroup();

138             buttonJoin.Enabled = true;

139             buttonEnd.Enabled = false;

140             buttonSend.Enabled = false;

141             this.AcceptButton = buttonJoin;

142             this.CancelButton = buttonExit;

143             this.Close();

144         }

145 

146         public void displayMessage(string message)

147         {

148             lstOngoing.Items.Add(message);

149             lstOngoing.Refresh();

150         }

151 

152         private void buttonJoin_Click(object sender, EventArgs e)

153         {

154             this.JoinGroup();

155         }

156     }

157 }

Here is a screen shot of my application connected to another member of the class.

Multicast Network Chat Screenshot

Leave a Reply


Switch to our mobile site