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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:23:32+00:00 2026-05-29T11:23:32+00:00

I have been looking at how calling BeginReceive on a .NET CF socket can

  • 0

I have been looking at how calling BeginReceive on a .NET CF socket can throw an SocketException as per http://msdn.microsoft.com/en-us/library/dxkwh6zw(v=vs.90).aspx?

I looked through the Reflector but I cannot see how this is possible? I think the SocketException is only possible while actually doing the work in a threadpool thread.

My guess is any SocketException is channel through EndReceive().

I have a callback method which calls itself (i.e. BeginReceive(.., .., callback), which contains an EndReceive. I want to make sure that I add a Try-catch in the right place.

Any ideas?

  • 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-29T11:23:33+00:00Added an answer on May 29, 2026 at 11:23 am

    I looked at the .NET Reference Source and it will throw an exception.

    Code:

    [HostProtection(ExternalThreading=true)] 
        public IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state)
        { 
            SocketError errorCode;
            IAsyncResult result = BeginReceive(buffer, offset, size, socketFlags, out errorCode, callback, state);
            if(errorCode != SocketError.Success && errorCode !=SocketError.IOPending){
                throw new SocketException(errorCode); 
            }
            return result; 
        } 
    

    In a method that gets called from overloads of the Begin in a method called DoBeginReceive this method calls this code. The errorcode is passed using the out keyword so it is exactly what is passed back.

    errorCode = UnsafeNclNativeMethods.OSSOCK.WSARecv(
                    m_Handle, 
                    ref asyncResult.m_SingleBuffer,
                    1,
                    out bytesTransferred,
                    ref socketFlags, 
                    asyncResult.OverlappedHandle,
                    IntPtr.Zero); 
    

    EDIT: For .NET CF:

    As Stated by the OP

    This is the Begin Receive

    public IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state)
    {
        this.throwIfDisposed();
        if (buffer == null)
        {
            throw new ArgumentNullException("buffer");
        }
        if (offset < 0)
        {
            throw new ArgumentOutOfRangeException("offset");
        }
        if (size < 0)
        {
            throw new ArgumentOutOfRangeException("size");
        }
        if (buffer.Length < (offset + size))
        {
            throw new ArgumentOutOfRangeException("", SR.GetString(0x41, new object[0]));
        }
        ReceiveAsyncRequest req = new ReceiveAsyncRequest(this, buffer, offset, size, socketFlags, callback, state);
        this.addReadRequest(req);
        return req;
    }
    

    The addReadRequest does this:

    private void addReadRequest(AsyncRequest req)
    {
        lock (this)
        {
            if (this.m_readHead == null)
            {
                if (!startWorker(req))
                {
                    req.InvokeCallback(false, new OutOfMemoryException(SR.GetString(0x42, new object[0])));
                }
                else
                {
                    this.m_readHead = req;
                }
            }
            else
            {
                AsyncRequest readHead = this.m_readHead;
                while (readHead.m_next != null)
                {
                    readHead = readHead.m_next;
                }
                readHead.m_next = req;
            }
        }
    }
    

    The startWorker does this:

    private static bool startWorker(AsyncRequest req)
    {
        WorkerThread thread = new WorkerThread(req);
        if (!ThreadPool.QueueUserWorkItem(new WaitCallback(thread.doWorkI)))
        {
            return false;
        }
        return true;
    }
    

    The Thread calls the doWork of the ReceiveAsyncRequestcalls the doRequest of the ReceiveAsyncRequestwhich calls the handleRequest which in turn calls doRequest on the ReceiveAsyncRequest.

    The doRequest of the ReceiveAsyncRequest method looks like this:

    protected override object doRequest()
    {
        return base.m_socket.ReceiveNoCheck(this.m_readBuffer, this.m_index, this.m_size, this.m_flags);
    }
    

    In the ReceiveFromNoCheck it throws the exception:

    internal int ReceiveFromNoCheck(byte[] buffer, int index, int request, SocketFlags flags)
    {
        this.throwIfDisposed();
        int rc = 0;
        int num2 = OSSOCK.recv(this.handle, buffer, index, request, (int) flags, ref rc);
        if(num2 < 0)
        {
            throw new SocketException(rc);
        }
        return num2;
    }
    

    If the AsyncResults runs sychronously, it will throw the exception. However, if it is part of the ThreadPool, the exception will just be thrown and kill the application.

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

Sidebar

Related Questions

I have been looking into IKVMing Apache's FOP project to use with our .NET
I have been looking at various dependency injection frameworks for .NET as I feel
I have been looking all over for an answer to this question, but can't
Have been looking at the MVC storefront and see that IQueryable is returned from
We have been looking at g++ versions 3.2.3 and 4.2.4. With 4.2.4, the performance
I have been looking at metrics for coupling and also look at DSM .
I have been looking for documentation related to interacting with MSPaint from the command
I have been looking at jQUery thickbox for showing modal dialogs with images, it
I have been looking at the new database server we are setting up for
I have been looking in to doing some test driven development for one of

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.