public static bool ReinitializeSocket()
{
// Save the state of the flag that indicates that data is to be
// acquired
bool Listen_for_Data_Was_On = SharedData.bool_Listen_for_Data;
bool Reinitialize_Result = true;
// If data is being acquired then stop
// acquistion and wait for a 1 second
if (Listen_for_Data_Was_On == true)
{
SharedData.bool_Listen_for_Data = false;
Thread.Sleep(1000);
}
// If socket is opened then close it
if (sock != null)
sock.Close();
// Open up a new socket with the net IP address and port number
try
{
sock = new UdpClient();
sock.ExclusiveAddressUse = false;
iep = new IPEndPoint(IPAddress.Any, SharedData.Current_Port);
sock.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sock.ExclusiveAddressUse = false;
sock.Client.Bind(iep);
sock.JoinMulticastGroup(IPAddress.Parse(SharedData.CurrentMulticastAddress), IPAddress.Parse(SharedData.CurrentInterfaceIPAddress));
}
catch
{
MessageBox.Show("ASTERIX ReinitializeSocket: Not possible! Make sure given IP address/port is a valid one on your system or not already used by some other process");
Reinitialize_Result = false;
}
// Everything is done, now resume listening (data acqusition) if it was active
// before new data was entered.
if (Listen_for_Data_Was_On == true)
SharedData.bool_Listen_for_Data = true;
return Reinitialize_Result;
}