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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:06:18+00:00 2026-05-22T20:06:18+00:00

Is it a good idea in my ReceiveCallBack of an asynchronous socket to Lock()

  • 0

Is it a good idea in my ReceiveCallBack of an asynchronous socket to Lock() the socket there? I’m asking because it’s possible that another thread is sending data on the socket at the same time.

private void ReceiveCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;

lock(client)
{
    int bytesRead = client.EndReceive(ar);
    // do some work

    // Kick off socket to receive async again.
    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReceiveCallback), state);
}
}    

// This is commonly called by another thread
public void SendMessage(string cmdName, Object data)
{
    lock (client)
    {
         client.Send(arrayofdata, 0, arraylength, 0);
    }
}
  • 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-22T20:06:18+00:00Added an answer on May 22, 2026 at 8:06 pm

    If you want to make it thread-safe and be able to send and receive simultaneously you need to create two lock sync objects:

    private readonly object sendSyncRoot = new object();
    private readonly object receiveSyncRoot = new object();
    
    private void ReceiveCallback(IAsyncResult ar)
    {
        StateObject state = (StateObject)ar.AsyncState;
        Socket client = state.workSocket;
    
        lock (receiveSyncRoot)
        {
            int bytesRead = client.EndReceive(ar);
            // do some work
    
            // Kick off socket to receive async again.
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(ReceiveCallback), state);
        }
    }
    
    // This is commonly called by another thread
    public void SendMessage(string cmdName, Object data)
    {
        lock (sendSyncRoot)
            client.Send(arrayofdata, 0, arraylength, 0);
    }
    

    It is generally a good practice to have dedicated syncRoot objects instead of locking on other classes or members. This allows to avoid subtle deadlocks.

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

Sidebar

Related Questions

Is it a good idea to use sockets to send data between two servers,
I would like to know if there is any good idea how to switch
Are there some cases where it may not be a good idea to use
I've recently read that embedding ruby inside JavaScript is not a good idea. However,
Possible Duplicate: Is a good idea have a BaseController and make all controllers extend
Is it a good idea to use while loop instead of a cursor? What
When is it a good idea to use PHP_EOL ? I sometimes see this
Is it a good idea to store my SQL queries in a global resource
Is it a good idea (from a design POV) to nest constructor calls for
Is it a good idea for me to use Qt Jambi in Java as

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.