Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7587623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:43:10+00:00 2026-05-30T19:43:10+00:00

I have 500 connected clients on the server. Server sends data to all clients

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:43:12+00:00Added an answer on May 30, 2026 at 7:43 pm

    I have some points for you:

    You can improve efficiency by getting rid of one superfluous task per send:

    public static Task<int> SendAsync(this Socket socket, byte[] buffer, int offset, int count)
    {
            return Task.Factory.FromAsync<int>(
                           socket.BeginSend(buffer, offset, count, SocketFlags.None, null, socket),
                     socket.EndSend);
    //Don't explicitly start the return value of this method. The task will already be started.
    }
    

    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:

    int totalConnectedClients = connectedClients.Count;
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 500 images named Image1.tif all the way to Image500.tif and I need
Suppose I have 500 rows of data, each with a paragraph of text (like
I have this table: id feed_id ... Let's say that I have 500 rows
I have a 500,000 line SQL script: UPDATE users SET region_id = 9814746 WHERE
I have about 100k Outlook mail items that have about 500-600 chars per Body.
Greetings. 1 - Let's say I have about 500 folders of variable size with
I have a directory with 500,000 files in it. I would like to access
I have raw observations of 500 numeric values (ranging from 1 to 25000) in
I have a middle size query with 500.000 registers from an Oracle to an
I have a 2D bitmap-like array of let's say 500*500 values. I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.