Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Something really strange:

 

	protected void WaitForData()
	{
		if(this.SocketCallback == null)
		{
			this.SocketCallback = new AsyncCallback(OnDataReceive);
		}
		
		ServerSocket state = new ServerSocket();
		state.Socket = this.Socket;
		
		this.Socket.BeginReceive(state.buffer1, 0, state.buffer1.Length, SocketFlags.None, this.SocketCallback, state);
	}
	
	public void OnDataReceive(IAsyncResult result)
	{
		ServerSocket socket = (ServerSocket) result.AsyncState;
		int bufferOffset = this.Socket.EndReceive(result);
		
		if(bufferOffset > 0 && bufferOffset != socket.buffer1.Length)
		{
			byte[] temp = new byte[bufferOffset];
			for(int i = 0; i < temp.Length; i++)
			{
				temp[i] = socket.buffer1[i];
			}
			
			this.buffer1 = temp;
			temp = null;
		}
	}

 

I get an ObjectDisposedException at this line:

		int bufferOffset = this.Socket.EndReceive(result);

 

But:

	protected void WaitForData()
	{
		if(this.SocketCallback == null)
		{
			this.SocketCallback = new AsyncCallback(OnDataReceive);
		}
		
		ServerSocket state = new ServerSocket();
		state.Socket = this.Socket;
		
		this.Socket.BeginReceive(state.buffer1, 0, state.buffer1.Length, SocketFlags.None, this.SocketCallback, state);
		MessageBox.Show("Foo");
	}
	
	public void OnDataReceive(IAsyncResult result)
	{
		ServerSocket socket = (ServerSocket) result.AsyncState;
		int bufferOffset = this.Socket.EndReceive(result);
		
		if(bufferOffset > 0 && bufferOffset != socket.buffer1.Length)
		{
			byte[] temp = new byte[bufferOffset];
			for(int i = 0; i < temp.Length; i++)
			{
				temp[i] = socket.buffer1[i];
			}
			
			this.buffer1 = temp;
			temp = null;
		}
	}

(Note de MessageBox in WaitForData())

 

Works perfect.

 

What's wrong?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...