How to cancel a pending BeginReceive method of Socket class in .NET without using Close method ?

How to cancel a pending BeginReceive method of Socket class in .NET without using Close method ?
kalibukAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kalibukAuthor Commented:
Any help appreciated.
0
MajorBigDealCommented:
Hi Kalibuk,  do you any code written yet? Can you post it?  Is this a general inquiry or do you have a specific situation or bug that you are trying overcome?
0
kalibukAuthor Commented:
From my code I call attached function WaitForData(). When the caller decides to not wait any more, the callback OnDataReceived is still hanging in memory. Proper solution is to call Close() method on ((Socket)objResource), but I cannot lost handle to the opened socket or create another one.
private int WaitForData()
        {
            try
            {
                if (pfnCallBack == null)
                    pfnCallBack = new AsyncCallback(OnDataReceived);
                // now start to listen for any data...
                m_asynResult =
                ((Socket)objResource).BeginReceive(m_DataBuffer, 0, m_DataBuffer.Length, SocketFlags.None, pfnCallBack, null);
                return NTRESULT.OK;
            }
...
}

Open in new window

0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

MajorBigDealCommented:
Just asking so I can understand the situation better:

1. What is the reason you don't want to specify the timeout?

2. What is the reason you can't create another socket?
0
MajorBigDealCommented:
It seems that the short answer is that you can't:

http://stackoverflow.com/questions/563593/cancelling-a-socket-xxxxasync-call

However, there might be a slightly different way to accomplish your goal. That's why I'm asking the above question.  Just trying to understand what you are trying to do.

I suppose you could abort the thread but I would not design it that way.  Another choice would be to use a non-blocking socket instead of async:

http://stackoverflow.com/questions/5743304/non-blocking-udp-socket-net-a-non-blocking-socket-operation-could-not-be-compl
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MajorBigDealCommented:
Please disregard the comment about aborting the thread, that won't help and is a very bad idea.
0
kalibukAuthor Commented:
Hallo,
so as you suggested we abandoned idea of async receive while there is no easy way to kill pending method. I rewrote the code to use simple Receive method in blocking mode  and now I'm able to cancel Receive method on custom event I get from caller. So thanks!
0
MajorBigDealCommented:
Glad I was able to assist - and thanks for asking such an interesting question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.