Reputation: 31
I have a sever that allows me to connect several clients receive a message from client and send every client a message when it connects. What I'm trying to do is send a message from client to another so all the server is supposed to do is get the message from first client and send to another. How can I do that?
This is the code of the server
class Server
{
private TcpListener tcpListener; private Thread listenThread;
public Server()
{
this.tcpListener = new TcpListener(IPAddress.Any, 3000);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch (Exception ex)
{
//a socket error has occured
Console.WriteLine(ex.Message);
}
if (bytesRead == 0)
{
//the client has disconnected from the server
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
Console.WriteLine(encoder.GetString(message, 0, bytesRead));
byte[] buffer = encoder.GetBytes("Hello Client!");
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
tcpClient.Close();
}
static void Main(string[] args)
{
new Server();
}
}
and the client
{
TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);
client.Connect(serverEndPoint);
NetworkStream clientStream = client.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes("Hello Server!");
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
//message has successfully been received
byte[] message = new byte[4096];
int bytesRead;
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine(encoder.GetString(message, 0, bytesRead));
Console.ReadLine();
}
Upvotes: 0
Views: 2435
Reputation: 61349
You have to hold the incoming TcpClient
objects in memory so that you can send to them later.
Something like:
List<TcpClient> connectedClients = new List<TcpClient>();
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
connectedClients.Add(client);
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
Then you can get the client out of the list (or just send to the whole list) normally. You may want to encapsulate the TcpClient
into a full-fledged "Client" object so you can keep track of what they send/receive and also send messages to specific clients.
Upvotes: 1