private void btnAdd_Click(object sender, EventArgs e)
{
bool Input_Validated = true;
// First make sure that all boxes are filled out
if ((!string.IsNullOrEmpty(this.textBoxConnectionName.Text)) &&
(!string.IsNullOrEmpty(this.txtboxIPAddress.Text)) &&
(!string.IsNullOrEmpty(this.comboBoxNetworkInterface.Text)) &&
(!string.IsNullOrEmpty(this.textboxPort.Text)))
{
IPAddress IP;
IPAddress Multicast;
// Validate that a valid IP address is entered
if ((IPAddress.TryParse(this.txtboxIPAddress.Text, out Multicast) != true) || (IPAddress.TryParse(this.comboBoxNetworkInterface.Text, out IP) != true))
{
MessageBox.Show("Not a valid IP address");
Input_Validated = false;
}
else // Add a check that this is a valid multicast address
{
UdpClient TempSock;
TempSock = new UdpClient(2222);// Port does not matter
// Open up a new socket with the net IP address and port number
try
{
TempSock.JoinMulticastGroup(Multicast, 50); // 50 is TTL value
}
catch (Exception e1)
{
MessageBox.Show("Not valid Multicast address (has to be in range 224.0.0.0 to 239.255.255.255");
// Just to avoid warning that e1 is not used.
string temp = e1.Message;
Input_Validated = false;
}
TempSock.Close();
}
int PortNumber;
if (int.TryParse(this.textboxPort.Text, out PortNumber) && (PortNumber >= 1 && PortNumber <= 65535))
{
}
else
{
MessageBox.Show("Invalid Port number");
Input_Validated = false;
}
}
else
{
MessageBox.Show("Please fill out all data fileds");
Input_Validated = false;
}
if (Input_Validated == true)
{
this.textBoxConnectionName.Text = this.textBoxConnectionName.Text.Replace(' ', '_');
string ConnInfo = this.textBoxConnectionName.Text;
this.listBoxConnName.Items.Add(ConnInfo);
ConnInfo = this.comboBoxNetworkInterface.Text;
this.listBoxLocalAddr.Items.Add(ConnInfo);
ConnInfo = this.txtboxIPAddress.Text;
this.listBoxIPAddress.Items.Add(ConnInfo);
ConnInfo = this.textboxPort.Text;
this.listBoxPort.Items.Add(ConnInfo);
}
// The last thing is to check if there is anything in the list, if so then enable the button
// to allow setting an active multicast address
if (this.listBoxConnName.Items.Count > 0)
{
this.buttonSetAsActive.Enabled = true;
this.listBoxConnName.SelectedIndex = this.listBoxConnName.Items.Count - 1;
}
}