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 8748995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:37:13+00:00 2026-06-13T12:37:13+00:00

Why am I getting an error of BeginReceive method of Size (3rd parameter)? But

  • 0

Why am I getting an error of BeginReceive method of Size (3rd parameter)?

But BUFFER_SIZE its never bigger (in lenght) then the constant value (its 2048 bytes).

This is my code:

public void Send(byte[] byteData, M.StateObject _stateObj, Socket client)
    {
        // Begin sending the data to the remote client(s):
        client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendData), _stateObj);
    }

    void SendData(IAsyncResult ar)
    {
        try
        {
            M.StateObject stateObj = (M.StateObject)ar.AsyncState;
            Socket client = stateObj.server;
            byte[] data = stateObj.data;
            int bytesSent = client.EndSend(ar);
            client.BeginReceive(data, 0, M.StateObject.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveData), stateObj);
            allDone.Set();
        }
        catch (Exception ex)
        {
            OnMessageShowing(ex.Message);
        }
    }

    private void ClientLogging(object _data, M.StateObject _stateObj)
    {
        M.UserData client = _data as M.UserData;
        if (client != null)
        {
            //setting properties:
            client.bConnectred = true;

            // 1. add client to list of clients:
            ClientAddingRemoving(_stateObj, client, true);

            // 2. showing welcome on server
            OnMessageShowing(client.ServerNotifying());

            // 3. refreshing the list of clients on server`s main form
            OnClientsListRefreshing(bll.ClientsListCreating(listOfClients));

            // 4. send welcome note to all clients:
            byte[] bytesToSend = M.SerializeServer.Login_WelcomeToAllClients(client);
            bytesToSend = M.BytesArrayGetSet.BytesSending_Set(bytesToSend);
            SendToAll(bytesToSend, _stateObj.server);

            // 5. send list of all users to logged client:
            bytesToSend = M.SerializeServer.Login_ToLoggedClient(bll.ListOfAllUsers(listOfClients));
            bytesToSend = M.BytesArrayGetSet.BytesSending_Set(bytesToSend);
            Send(bytesToSend, _stateObj, _stateObj.server);

            //6. send new client to list of all other clients:
            bytesToSend = M.SerializeServer.Login_ToAllClients(client);
            bytesToSend = M.BytesArrayGetSet.BytesSending_Set(bytesToSend);
            SendToAll(bytesToSend, _stateObj.server);
        }
    }

    private void SendToAll(byte[] data, Socket _socket)
    {
        foreach (M.StateObject stObj in listOfClients.Keys) //dictionary<StateObject, Client>, Client is a custom client class
        {
            Send(data, stObj, _socket);
        }
    }

Here is the StateObject class:

public class StateObject 
{    
    public Socket Server;
    public const int BUFFER_SIZE = 2048;
    public byte[] data = new byte[BUFFER_SIZE];
}

Maybe calling Send() method too many times? Do I have to use “ManualResetEvent” delegate to block or to do something else? I really dont know what to do.

  • 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-06-13T12:37:14+00:00Added an answer on June 13, 2026 at 12:37 pm

    Replace M.StateObject.BUFFER_SIZE with stateObj.data.Length, somehow data is smaller than BUFFER_SIZE.

    Also I see you put out the data in to an array but don’t do anything with it. I would reccomend changing this

    byte[] data = stateObj.data;
    int bytesSent = client.EndSend(ar);
    client.BeginReceive(data, 0, M.StateObject.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveData), stateObj);
    

    to this

    int bytesSent = client.EndSend(ar);
    client.BeginReceive(stateObj.data, 0, stateObj.data.Length, SocketFlags.None, new AsyncCallback(ReceiveData), stateObj);
    

    and see if that fixes your error.


    Also I noticed that Your BeginSend is passing in the state object blob. Is the array you used to send data stateObj.data too? And if so, are multiple calls to BeginSend using new stateObj objects? If they are all sharing the same state object you could have the next send reading the same time as the current receive is writing.

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

Sidebar

Related Questions

Update getting error when i try to call the method from Activity: SQLiteDatabase db
I'm getting error C3095: 'Xunit::Extensions::InlineDataAttribute': attribute cannot be repeated in C++/CLI code but not
PHP Uploading issue Getting Error 0 but move_uploaded_file() is returning false. When I print
I'm getting error from devise User does not respond to 'devise' method when running
I'm getting error at User.findById(user_id) not frequently but some times, I don't know why.
I am getting error as Function 'testMethod' doesn't exist in SOAP UI method response.
Getting Error while running rhinomock test method : Test method TestProject1.UnitTest2.TestMethod1 threw exception: Rhino.Mocks.Exceptions.ExpectationViolationException:
I m getting error at setcontentview, its not taking layout main.Resource file is doesn't
I am getting #error WINDOWS.H already included. MFC apps must not #include windows.h But
Getting error at line [theSelectedRoutine addExerciseObject: exercise]; saying method -addExerciseObject not found. Interface: @property

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.