A
Adam
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);
}
}
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);
}
}