MultiCasting Problem.

Most of you have seen this code posted below on the the net. I'm having problems with the thread, why will the thread not close properly Is there something here that you see that would be causing the issue It works great in a console application, but in a windows connection it faults on the stop method

I await your reply.

public class MCast

{

private UdpClient _client;

private int _listenerport = 8080;

private int _senderport = 8080;

private int _ttl = 20; //Time to Live

private bool _started = false;

private int LocalPort;

private int RemotePort;

private string _message = "";

private bool done = true;

private Thread t;

Encoding ASCII = Encoding.ASCII;

private string _szhostname;

private string _szusername;

private IPAddress _groupaddress = IPAddress.Parse("224.0.0.1");

private IPHostEntry _localhost;

private IPEndPoint _remoteep;

public event EventHandler Receive;

public void ReceiveMessage()

{

Receive(this, EventArgs.Empty);

}

public int ListenerPort

{

get { return _listenerport; }

set { _listenerport = value; }

}

public int SenderPort

{

get { return _senderport; }

set { _senderport = value; }

}

public int TTL

{

get { return _ttl; }

set { _ttl = value; }

}

public string GroupAddress

{

get { return _groupaddress.ToString(); }

set {_groupaddress = IPAddress.Parse(value);}

}

public string Message

{

get { return _message; }

set { _message = value; }

}

public string HostName

{

get { return _szhostname; }

}

public bool Initialize()

{

//Set Local and Remote Port Private Variables

LocalPort = _senderport;

RemotePort = _listenerport;

//instatitate UdpClient

_client = new UdpClient(LocalPort);

//Join Group

try

{

_client.JoinMulticastGroup(_groupaddress, _ttl);

}

catch

{

//throw new Exception("Unable ToString join MulticastDelegate group!", ex);

return false;

}

//Create Endpoint for peer

_remoteep = new IPEndPoint(_groupaddress, RemotePort);

_szhostname = Dns.GetHostName();

_localhost = Dns.GetHostEntry(_szhostname);

_szusername = Environment.UserName;

return true;

}

public bool Start()

{

try

{

t = new Thread(new ThreadStart(Listener));

t.IsBackground = true;

t.Start();

Encoding ASCII = Encoding.ASCII;

_started = true;

return true;

}

catch

{

MessageBox.Show("There is an Error!", "MCast.Start");

return false;

}

}

public void Stop()

{

try

{

_client.DropMulticastGroup(_groupaddress);

_client.Close();

t.Abort();

t.Join();

done = true;

}

catch { MessageBox.Show("There is an Error!", "MCast.Stop"); }

}

public bool SendMsg(string Message)

{

Byte[] buffer = null;

if (_started && Message.Length > 0)

{

try

{

//Send message to Group

buffer = new Byte[Message.Length + 1];

int len = ASCII.GetBytes(Message.ToCharArray(), 0, Message.Length, buffer, 0);

int ecode = _client.Send(buffer, len, _remoteep);

if (ecode <= 0)

return false;

else

return true;

}

catch

{

MessageBox.Show("There is an Error!", "MCast.SendMsg");

return false;

}

}

return false;

}

public void Listener()

{

done = false;

Thread.Sleep(2000);

try

{

while (!this.done)

{

if (this.done == true)

break;

IPEndPoint ep = null;

Byte[] data = _client.Receive(ref ep);

string strData = ASCII.GetString(data);

Message = strData;

ReceiveMessage();

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message, "Error MCast: Listener",

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

MessageBox.Show("I'm Out of the Thread, this is not the Problem!", "PlotMgrlib");

}

} //End Class MCast



Answer this question

MultiCasting Problem.

  • RajLakamana

    Could you provide a little more information about the error you are getting

    Keep in mind that, when you abort a thread, a ThreadAbortedException is raised on the thread you are aborting. Alternatively, you might get a SocketException, due to the fact that you are aborting a blocking operation.

    HTH
    --mc


  • reichard

    I appreciate your reply,

    I do see that the ThreadAbortedException is raised, and I figured that is normal and could be ignored But the one that concerns me the most is that I do get a message: A blocking operation was interrupted by a call to WSACancelBlockingCall.

    I'm wondering if there is a better way to handle the error or re-write the code because although the window closes, the program remains in memory. I do know that calling the abort method doesn't always stop the thread immediately, but if the program remains in memory, that's not good either.

    If you need more information, please let me know,

    Thanks


  • MultiCasting Problem.