public static void Listener()
{
bool Done = false;
string stringData;
byte [] data;
// The listener waits for data to come and buffers it
Thread.Sleep(2000); // Make sure client2 is receiving
Encoding ASCII = Encoding.ASCII;
Console.WriteLine("\nReady to transmit input...\n");
lock(typeof(Chat))
{
Done = m_Done;
}//lock
//while(!m_Done)
while(!Done)
{
IPEndPoint endpoint = null;
//Can't lock here because thread waits on the Receive before continuing.
data = m_Client.Receive(ref endpoint);
string strData = ASCII.GetString(data);
if( strData.IndexOf(":@") > 0 )
{
//
// We received a termination indication
// now we have to decide if it is from
// our main thread shutting down, or
// from someone else.
//
char [] separators = {':'};
string [] vars = strData.Split(separators);
lock(typeof(Chat))
{
stringData = m_szHostName;
}//lock
if( vars[0] == stringData )
{
//
// This is from ourselves, therefore we end now.
//
Console.WriteLine("Shutting down Listener thread...");
//
// This should have been done by main thread, but we
// do it again for safety.
//
Done = true;
lock(typeof(Chat))
{
m_Done = true;
}//lock
}//if
else
{
//
// This is a termination from someone else
//
Console.WriteLine("{0} has left the conversation", vars[0]);
}//else
}//if
else
{
//
// this is normal data received from others
// as well as ourselves
// check to see if it is from ourselves before
// we print
//
if(strData.IndexOf(":") > 0)
{
char [] separators = {':'};
string [] vars = strData.Split(separators);
lock(typeof(Chat))
{
stringData = m_szHostName;
}//lock
if( vars[0] != stringData )
{
Console.WriteLine(strData);
}//if
}//if
}//else
}//while
Console.WriteLine("Listener thread finished...");
return;
}//Listener()
}//class Chat