Perry

Perry
- Name: [not set]
- Favorite Languages: C#, html, javascript, css, actionscript 2&3
- Website: [not set]
- Location: Behind my Pc
- About Me: [not set]
-
C# Tutorial - Simple Threaded TCP Server
05/09/2010 - 11:32
Don't know if I'm asking the same as the guy above me, but I'm asking it more simple anyway. How do I send to all connected clients? I've tried adding all tcpClients to a list and I tried to send to their streams, but unfortunatly I don't recieve a thing in my client, while I can send data back to a single client upon connection...
Here's the 'sending' code:
public void sendToClient(String line, TcpClient client)
{
try
{
NetworkStream stream = client.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes(line + "\0");
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
}
catch
{
connClients.Remove(client);
myInterface.logTxt.Invoke(new AppendTextDelegate(writeIntoLog), ("Client Disconnected."+Environment.NewLine));
}
}
public void broadcast(String line)
{
for (int i = 0; i < connClients.Count; i++)
{
sendToClient(line, connClients[i]);
}
myInterface.logTxt.AppendText("Message broadcasted to " + connClients.Count.ToString() + " clients." + Environment.NewLine);
}
}
Recent Comments