I have 500 connected clients on the server. Server sends data to all clients in every N sec.
When the server sends data to clients the result is:
-----------Result-----------
Client1 received : 102 datas
Client2 received : 109 datas
Client3 received : 105 datas
Client4 received : 108 datas
Client5 received : 108 datas
ClientN received : 107 datas
But it should be 110 datas at the end.
I am using the following code to send data to clients
public static class AsyncSend
{
public static Task<int> SendAsync(this Socket socket, byte[] buffer, int offset, int count)
{
return new Task<int>(() =>
{
return Task.Factory.FromAsync<int>(
socket.BeginSend(buffer, offset, count, SocketFlags.None, null, socket),
socket.EndSend).Result;
});
}
}
class ServerTest
{
private Thread sendThread;
private Dictionary<String, Client> connectedClients;
public ServerTest()
{
connectedClients = new Dictionary<String, Client>();
}
public void receive()
{
//here server receives data from another server
//When server receives new data then fires the newDataReceived(String data) method.
newDataReceived(data);
}
public void newDataReceived(String data)
{
sendThread = new Thread(new ParameterizedThreadStart(sendToClients));
sendThread.Start(data);
}
private void sendToClients(object dataObj)
{
try
{
int totalConnectedClients = connectedClients.Count;
if (totalConnectedClients > 0)
{
lock (connectedClients)
{
byte[] buffer = Encoding.UTF8.GetBytes((string)dataObj);
byte[] cBuffer = new SCompressor().Compress(buffer);
foreach (Client client in connectedClients.Values)
{
Task<int> task = AsyncSend.SendAsync(client.Socket, cBuffer, 0, cBuffer.Length);
task.Start();
}
}
}
}
catch (Exception ex)
{
//ERROR.
}
}
Is there another way or how to send data to multiple clients correctly and efficiently?
Please show me examples if you can.
Thanks in advice.
I have some points for you:
You can improve efficiency by getting rid of one superfluous task per send:
You usage of asynchronous sending is correct as far as I can tell (except for the unneeded task).
Are you using UDP? If yes, that would explain why some data is missing.
Is there an error that is being caught? If yes, the loop will abort and not all clients will get their data.
You have a race condition:
This should happen inside the lock. You cannot safely read Count while other threads might update this dictionary.
How are you receiving the data? Maybe the bug is on the receive-side. Please post some code.
Edit:
What if two concurrent receives are running? This would cause a bug: Clients could receive the newest data first, then the older data second. I recommend that you do not start a new thread every time you receive data. Instead, have a thread running permanently which is pulling new data from a BlockingCollection using the GetConsumerEnumerable method. Your newDataReceived method would just enqueue into this BlockingCollection. You can find an excellent introduction here: http://blogs.msdn.com/b/csharpfaq/archive/2010/08/12/blocking-collection-and-the-producer-consumer-problem.aspx