Jump to content
  • Advertisement
Sign in to follow this  

[.net] BeginSend() returning possible error

This topic is 4448 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having a problem with this code:
		public void WriteLine(Socket SocketHandle, string Data)
		{
			byte[] DataArray = new byte[Data.Length];
			DataArray = Encoding.ASCII.GetBytes(Data.ToCharArray());
			
			SocketHandle.BeginSend(DataArray, 0, DataArray.Length, SocketFlags.None, new AsyncCallback(WriteCallback), SocketHandle);
		}
		
		private void WriteCallback(IAsyncResult ar)
		{
			Socket s = (Socket)ar.AsyncState;
			s.EndConnect(ar);
		}
MSVS 2005 Express tells me that when I call WriteLine(), the BeginSend function doesn't return anything. The actual error message looks like this: "Exception System.ArgumentException was thrown in debugee: The IAsyncResult object was not returned from the corresponding asynchronous method on this class." That error message points to the line that goes: "s.EndConnect(ar);" I'm suspecting that BeginReceive() is returning an error, but I've no idea how to check for it. It should probably be done in the callback function, but what form is the error going to be in? Also, if an error is generated, then why is that? Can anyone see anything wrong with my code? Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Um, that's because EndConnect() should be called in the callback to BeginConnect()! If you're trying to disconnect the socket after writing, use Close() instead.

Share this post


Link to post
Share on other sites
:O

That worked! Thanks alot!

Quote from MSDN:

[C#] 
allDone.Set();
Socket s = (Socket) ar.AsyncState;
s.EndConnect(ar);
StateObject so2 = new StateObject();
so2.workSocket = s;
byte[] buff = Encoding.ASCII.GetBytes("This is a test");
s.BeginSend(buff, 0, buff.Length,0,
new AsyncCallback(Async_Send_Receive.Send_Callback), so2);


MSDN Article on BeginSend

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!