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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:07:03+00:00 2026-06-01T16:07:03+00:00

Another networking question this time related to an Async Socket Client which is based

  • 0

Another networking question this time related to an Async Socket Client which is based on the MSDN examples at least for this initial version. Currently when the user clicks a button on the interface an Async Connect attempt is made to connect to a networked device, the code is shown below –

//Mouse Event handler for main thread
private void btn_Read_MouseDown(object sender, MouseEventArgs e)
{
    Stopwatch sw = Stopwatch.StartNew();
    if (!networkDev.Connected)
        networkDev.Connect("192.168.1.176", 1025);

    if(networkDev.Connected)
       networkDev.getReading();
    sw.Stop();//Time time taken...
}

If the end point is switched on and exists on the network this code works fine (less than a second for whole operation). However should the networked device be switched off or unavailable the AsyncSocket Connect function holds up the main forms thread. Currently if the device is unavailable the whole interface locks up for about 20 seconds (using the stopwatch). I think that I am locking because the main thread is waiting for the return on the Connect request, does this mean I need to put that connect request on another thread?

I have included the code the Async Socket Client I am using –

    public bool Connect(String ip_address, UInt16 port)
    {
        bool success = false;

        try
        {
            IPAddress ip;
            success = IPAddress.TryParse(ip_address, out ip);
            if (success)
                success = Connect(ip, port);
        }
        catch (Exception ex)
        {
            Console.Out.WriteLine(ex.Message);
        }
        return success;
    }     

    public bool Connect(IPAddress ip_address, UInt16 port)
    {
        mSocket.BeginConnect(ip_address, port, 
           new AsyncCallback(ConnectCallback), mSocket);
        connectDone.WaitOne();//Blocks until the connect operation completes, 
                              //(time taken?) timeout?
        return mSocket.Connected;
    }

    private void ConnectCallback(IAsyncResult ar)
    {
        //Retreive the socket from thestate object
        try
        {
            Socket mSocket = (Socket)ar.AsyncState;
            //Set signal for Connect done so that thread will come out of 
            //WaitOne state and continue
            connectDone.Set();

        }
        catch (Exception ex)
        {
            Console.Out.WriteLine(ex.Message);
        }       
    }

I was hoping that by using an Async client which has its own thread that this would then stop freezing the interface if the host didn’t exist but this does not appear to be the case. After the initial connect failure which takes 20s all subsequent connection attempts are returned immediately (less than a ms). Also something I thought strange, if the initial connection attempt succeeds, any later calls to connect to a non existent host return immediately. A little baffled by what is happening but wondering if its related to the fact that the Socket I use is stored in my AsyncSocket class. Any help much appreciated, if more of the client code is required let me know.

  • 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-01T16:07:04+00:00Added an answer on June 1, 2026 at 4:07 pm

    You claim it’s asynchronous, but your Connect method clearly isn’t:

    mSocket.BeginConnect(ip_address, port, ...);
    connectDone.WaitOne(); // Blocks until the connect operation completes [...]
    

    You’re blocking until it’s completed, which is the antithesis of asynchronous behaviour. What’s the point of using BeginConnect if you’re going to block until it’s connected?

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

Sidebar

Related Questions

Another question related to this one . I have a List<SortableObjects> that is the
another sql problem of mine .. this time the reader doesn't work properly (
I've seen The Coolest Server Names , and I've seen another smaller-ish question related
Another easy one hopefully. Let's say I have a collection like this: List<DateTime> allDates;
Another poster asked about preferred syntax for infinite loops . A follow-up question: Why
Another question asked about determining odd/evenness in C, and the idiomatic (x & 1)
Another basic Rails question: I have a database table that needs to contain references
Another clipboard question: When text is put onto the clipboard, it frequently goes in
I am building a networking website which allows users to upload images. When one
I ran across the error Socket operation on non-socket in some of my networking

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.