Click here to Skip to main content
12,892,796 members (47,995 online)

Call BeginRecieve from within the BeginAccept Delegate, guaranteed completion?

cognismith asked:

Open original thread
I'm writing a server application which uses the .net sockets to communicate using the provided Async functionality, and I have a question regarding the reliability of the method I am using.

MSDN - Socket.EndReceive Method (IAsyncResult)[^] Notes that:
"All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes."

With this in mind, if I call my BeginRecieve() function from within the delegate that is called by BeginAccept(), Can I rely on the async receive function successfully completing, given that I have no control over the threadpool iocp uses in the background to run the socket async events?
(noob note: I'm still not 100% on exactly how it all goes together, please correct me if I am wrong)

Also, if it's not reliable, how would I be better off calling the receive function?

Thanks in advance.

basic outline of code:
private void ClientAcceptorDelegate()
	while (AcceptingConnections)
		//[omitted code]
		m_MainSocket.BeginAccept(new AsyncCallback(AcceptClientCallback), m_MainSocket);
		//[omitted code]
void AcceptClientCallback(IAsyncResult AR)
	Socket ClientSocket = (Socket) AR.AsyncState.EndAccept(AsyncResult);
	//[omitted code]
	ClientStateMachine ClientState = new ClientStateMachine(clientName);
	ClientSocket.BeginReceive(ClientState.m_Recievebuffer, 0, ClientState.m_bufferSize, SocketFlags.None, new AsyncCallback(ClientRecieveDataCallback), ClientState);
	//NOTE: this __might__ fail? IO operations will terminate with the calling thread, if iocp worker thread dissapears this might never complete?. (nbdf)
	//[omitted code]
void ClientRecieveDataCallback(IAsyncResult AR) 
	ClientStateMachine ClientState = (ClientStateMachine) AR.AsyncState;
	Socket ClientSocket = ClientState.socket;
	int bytesRead=0;
	//[omitted code]
	bytesRead = ClientSocket.EndReceive(AR);
	//[omitted code]
	//continue recieve process
	ClientSocket.BeginReceive(ClientState.m_Recievebuffer, 0, ClientState.m_bufferSize, SocketFlags.None, new AsyncCallback(ClientRecieveDataCallback), ClientState);
Tags: C#, .NET, Sockets, Asynchronous


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100