Note that there are some explanatory texts on larger screens.

plurals
  1. PO.net sockets ObjectDisposed exception
    text
    copied!<p>I am using async sockets in my project. </p> <p>When i calling Disconnect method in my class NotifyConnection ObjecDispoded exception arises. I realize that that happen becaouse socket.Close() method calls Dispose inside.</p> <p>Have anyone idea how close socket in this situation ?</p> <pre><code> public void Disconnect() { try { lock (_syncRoot) { _clientSocket.Shutdown(SocketShutdown.Both); _clientSocket.Close(); } } catch (SocketException ex) { OnSocketExeceptionThrowed(new EventArgs&lt;SocketException&gt;(ex)); NotificationAgentEm.LogExceptionToConsole(ex); } } </code></pre> <p>I expcect what EndReceive not called because socket.ShutDown closes socket receiving data..but EndReceive called after socket.ShutDown;socket.Close.</p> <p>End exception throwed because socket not exist at this moment.</p> <pre><code> private void OnReceiveData(IAsyncResult ar) { try { TransferStateObject state = null; lock(_syncRoot) { string message; state = (TransferStateObject)ar.AsyncState; // in this place exception throwed . client socket not exist becaouse it destroyed in disconnect method int bytesRead = _clientSocket.EndReceive(ar); </code></pre> <p>keleton of how i using async sockets.</p> <pre><code> public void Connect(string host, int port) { if (host == null) throw new NullReferenceException(); try { _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _clientSocket.Connect(host, port); } catch (Exception ex) { NotificationAgentEm.LogExceptionToConsole(ex); throw; } } public void Disconnect() { try { lock (_syncRoot) { _clientSocket.Shutdown(SocketShutdown.Both); _clientSocket.Close(); } } catch (SocketException ex) { OnSocketExeceptionThrowed(new EventArgs&lt;SocketException&gt;(ex)); NotificationAgentEm.LogExceptionToConsole(ex); } } public void StartListen() { if (_clientSocket == null) { throw new InvalidOperationException("No connection"); } try { BeginReceive(); } catch (SocketException ex) { NotificationAgentEm.LogExceptionToConsole(ex); OnSocketExeceptionThrowed(new EventArgs&lt;SocketException&gt;(ex)); } } private void BeginReceive() { try { var receivedTranferObject = new TransferStateObject(); _clientSocket.BeginReceive( receivedTranferObject.Buffer, 0, TransferStateObject.BufferSize, 0, new AsyncCallback(OnReceiveData), receivedTranferObject); } catch(SocketException ex) { OnSocketExeceptionThrowed(new EventArgs&lt;SocketException&gt;(ex)); NotificationAgentEm.LogExceptionToConsole(ex); } } private void OnReceiveData(IAsyncResult ar) { try { TransferStateObject state = null; lock(_syncRoot) { string message; state = (TransferStateObject)ar.AsyncState; // in this place exception throwed . client socket not exist becaouse it destroyed in disconnect method int bytesRead = _clientSocket.EndReceive(ar); //bla bla bla } } </code></pre> <p>p.s sorry for my english</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload