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

  • Home
  • SEARCH
  • 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 7547247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:24:13+00:00 2026-05-30T09:24:13+00:00

If I understood the logic of BeginSend correctly: private void sendToClient(Client client, String data)

  • 0

If I understood the logic of BeginSend correctly:

private void sendToClient(Client client, String data)
{
    byte[] byteData = Encoding.UTF8.GetBytes(data); //It must be UTF8

    client.BeginSend(byteData, 0, byteData.Length, 0, 
                     new AsyncCallback(sendCallback), client);
}
private void sendCallback(IAsyncResult iar)
{
    Socket client = (Socket)iar.AsyncState;
    int bytesSent = client.EndSend(iar);
}

I have sent the data to one client correctly.

But if I want to send the data for more clients:

/**
 * I am using dictionary, because I hold the clients with login-key
 */
private void SendToClients(Dictionary<string, Client> connectedClients, String data)
{ 
    foreach(Client client in connectedClients.Values)
    {
        sendToClient(client, data);
    }
}

Or in anotherway, I can use SocketAsyncEventArgs to send all clients:

private void sendToClients(Dictionary<string, Client> connectedClients, String data)
{
    SocketAsyncEventArgs asyncEvent;
    byte[] byteData = Encoding.UTF8.GetBytes(data); //It must be UTF8

    lock (connectedClients)
    {
        foreach (Client client in connectedClients.Values)
        {
            try
            {
                asyncEvent = new SocketAsyncEventArgs();
                asyncEvent.SetBuffer(byteData, 0, byteData.Length);
                client.Socket.SendAsync(asyncEvent);
            }
            catch (Exception ex)
            {
                //ERROR.
            }
        }
    }
}

But both sendToClients methods work slow for me and/or sometimes server can’t send the data to the any client. The packet can be lost.

Example:

Client1 Received 103 packets

Client2 Received 107 packets

Client3 Received 102 packets

Clientn Received 106 packets

But it should be 111 packets at the end.

Is there another methods/logics/technologies to send one data to more then one client or is there something used wrong in my examples?

Thanks in advance.

  • 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-30T09:24:15+00:00Added an answer on May 30, 2026 at 9:24 am

    That code looks ok, but it depends on the calling code initiating the multiple send calls. Note that you can’t safely call BeginSend multiple times before each previous operation has finished. See here:
    Async Socket Writes – Potential for send operations to get mixed up? (.NET Sockets). This may explain your non-deterministic packet received counts. So if you want to go that approach, you should probably use some mechanism that prevents multiple BeginSend calls before previous ones have finished.

    But what I have been doing lately is using the new TPL and its Task.Factory.FromAsync method to convert the old APM style Begin/End X to a Task<T>:

    http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx

    Once you have the Task object it is simple to schedule the next send (without overlapping the previous) by using the `Task.ContinueWith’ method.

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

Sidebar

Related Questions

As I have understood MVC, the logic for the model should also go into
I understood that anonymous types are marked private by the compiler and the properties
I've read somewhere that offline_access is/will be deprecated. If I understood correctly this means
As far as I have understood, dependency injection separates the application wiring logic from
I understand the need to test a class that has logic (for instance, one
I understand how NHibernate separates the persistent db structure from the logic and how
What's the difference between application layer and business logic layer? I kind of understand
I understood that this Bind attribute was not necessary, but JSON type is not
I understood,normally generics is for compile time safe and allow us to keep strongly
I haven't completely understood, how to use sigprocmask() . Particularly, how the set and

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.