By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

How to pass messages between multiple threads?

P: n/a
My telnet server app. creates a new thread for each incoming Tcp client as
shown in the code below. How can the client threads send text messages back
and forth to each other? I don't understand how separate threads can
communicate directly. Any techniques I can use?
Thanks!

//LISTENER THREAD
class Server
{
static void Main(string[] args)
{
Socketserver server=new Socketserver();
}

class Socketserver
{
int port=8000;
private TcpListener client;
public Socketserver()
{
client = new TcpListener(IPAddress.Any,port);
client.Start();
while(true)
{
while (!client.Pending())
{Thread.Sleep(1000);}
Newclientthread newconnection=new Newclientthread();
newconnection.threadListener=this.client;
//Create the client thread here for as many connections that
come in.
Thread newthread = new Thread(new
ThreadStart(newconnection.Sendreceiveloop));
newthread.Start();
}
}
}

//CLIENT THREAD SPAWNED BY THE LISTENER:
class Newclientthread
{
public TcpListener threadListener;
public static int connections=0;
public void Sendreceiveloop()
{
Socket ns=threadListener.AcceptSocket();
connections++;
try
{
Sendtext(ns,clientep,"Welcome to chat!!!!");
}
catch (Exception ex)
{
Disconnectclient(ns);
}
}




Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Here's a sample.
http://www.kbcafe.com/articles/HowTo....CSharp.II.pdf

Randy
http://www.kbcafe.com

"Adam" <no***********@comcast.net> wrote in message news:<iIz8b.428928$YN5.290602@sccrnsc01>...
My telnet server app. creates a new thread for each incoming Tcp client as
shown in the code below. How can the client threads send text messages back
and forth to each other? I don't understand how separate threads can
communicate directly. Any techniques I can use?
Thanks!

//LISTENER THREAD
class Server
{
static void Main(string[] args)
{
Socketserver server=new Socketserver();
}

class Socketserver
{
int port=8000;
private TcpListener client;
public Socketserver()
{
client = new TcpListener(IPAddress.Any,port);
client.Start();
while(true)
{
while (!client.Pending())
{Thread.Sleep(1000);}
Newclientthread newconnection=new Newclientthread();
newconnection.threadListener=this.client;
//Create the client thread here for as many connections that
come in.
Thread newthread = new Thread(new
ThreadStart(newconnection.Sendreceiveloop));
newthread.Start();
}
}
}

//CLIENT THREAD SPAWNED BY THE LISTENER:
class Newclientthread
{
public TcpListener threadListener;
public static int connections=0;
public void Sendreceiveloop()
{
Socket ns=threadListener.AcceptSocket();
connections++;
try
{
Sendtext(ns,clientep,"Welcome to chat!!!!");
}
catch (Exception ex)
{
Disconnectclient(ns);
}
}

Nov 15 '05 #2

P: n/a
Hello

You can use events as a method for thread communications.
See System.Threading.ManualResetEvent and System.Threading.AutoResetEvent
classes in .NET Framework documentation. You can have an arraylist to store
messages, a thread that waits for an event. When a thread wants to send a
message it puts the message in the arraylist, then sets the event, The
waiting thread can wakeup process all the messages in the list, reset the
event and wait again.

For performance consider using System.Threading.ThreadPool instead of
spawning a new thread for every connected client. Also use
System.Threading.Interlocked.Inecrement instead of connections++

Best Regards
Sherif
"Adam" <no***********@comcast.net> wrote in message
news:iIz8b.428928$YN5.290602@sccrnsc01...
My telnet server app. creates a new thread for each incoming Tcp client as
shown in the code below. How can the client threads send text messages back and forth to each other? I don't understand how separate threads can
communicate directly. Any techniques I can use?
Thanks!

//LISTENER THREAD
class Server
{
static void Main(string[] args)
{
Socketserver server=new Socketserver();
}

class Socketserver
{
int port=8000;
private TcpListener client;
public Socketserver()
{
client = new TcpListener(IPAddress.Any,port);
client.Start();
while(true)
{
while (!client.Pending())
{Thread.Sleep(1000);}
Newclientthread newconnection=new Newclientthread();
newconnection.threadListener=this.client;
//Create the client thread here for as many connections that
come in.
Thread newthread = new Thread(new
ThreadStart(newconnection.Sendreceiveloop));
newthread.Start();
}
}
}

//CLIENT THREAD SPAWNED BY THE LISTENER:
class Newclientthread
{
public TcpListener threadListener;
public static int connections=0;
public void Sendreceiveloop()
{
Socket ns=threadListener.AcceptSocket();
connections++;
try
{
Sendtext(ns,clientep,"Welcome to chat!!!!");
}
catch (Exception ex)
{
Disconnectclient(ns);
}
}



Nov 15 '05 #3

P: n/a
Adam, one way to do this communication would be with events. You could
create your own event with your own EventArgs. When you create each thread
make it listen for your event. When you want one thread to say something to
another thread just throw your event with the approriate EventArgs. Hope
that helps.

--
Greg Ewing [MVP]
http://www.citidc.com
"Adam" <no***********@comcast.net> wrote in message
news:iIz8b.428928$YN5.290602@sccrnsc01...
My telnet server app. creates a new thread for each incoming Tcp client as
shown in the code below. How can the client threads send text messages back and forth to each other? I don't understand how separate threads can
communicate directly. Any techniques I can use?
Thanks!

//LISTENER THREAD
class Server
{
static void Main(string[] args)
{
Socketserver server=new Socketserver();
}

class Socketserver
{
int port=8000;
private TcpListener client;
public Socketserver()
{
client = new TcpListener(IPAddress.Any,port);
client.Start();
while(true)
{
while (!client.Pending())
{Thread.Sleep(1000);}
Newclientthread newconnection=new Newclientthread();
newconnection.threadListener=this.client;
//Create the client thread here for as many connections that
come in.
Thread newthread = new Thread(new
ThreadStart(newconnection.Sendreceiveloop));
newthread.Start();
}
}
}

//CLIENT THREAD SPAWNED BY THE LISTENER:
class Newclientthread
{
public TcpListener threadListener;
public static int connections=0;
public void Sendreceiveloop()
{
Socket ns=threadListener.AcceptSocket();
connections++;
try
{
Sendtext(ns,clientep,"Welcome to chat!!!!");
}
catch (Exception ex)
{
Disconnectclient(ns);
}
}



Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.