Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

c# - System.ObjectDisposedException .NET Socket

My application crash this morning, and the log show.

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException
   at System.Net.Sockets.Socket.EndReceive(System.IAsyncResult, System.Net.Sockets.SocketError ByRef)
   at VN_PROXY.CORE_NETWORKING.SocketExtender+<>c__DisplayClass3_0.<RecvFromSocket>b__0(System.IAsyncResult)
   at System.Net.LazyAsyncResult.Complete(IntPtr)
   at System.Net.ContextAwareResult.CompleteCallback(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Net.ContextAwareResult.Complete(IntPtr)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr)
   at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

Here is my code of this part.

//Recv and Send to Socket operations
public static Task<int> RecvFromSocket(this Socket RECV_SOCKET, byte[] TransferBuffer, int Length)
{
    var tcs = new TaskCompletionSource<int>();
    if (RECV_SOCKET != null && RECV_SOCKET.Connected)
        RECV_SOCKET.BeginReceive(TransferBuffer, 0, Length, SocketFlags.None, out SocketError SOCKET_ERROR_HANDLER, result =>
        {
            Socket CLIENT_SOCKET = result.AsyncState as Socket;
            if (CLIENT_SOCKET.Connected)//After Dispose() is called .Connected property is set to false.
                tcs.SetResult(CLIENT_SOCKET.EndReceive(result, out SOCKET_ERROR_HANDLER));
            else tcs.SetResult(0);
        }, RECV_SOCKET);
    else tcs.SetResult(0);
    return tcs.Task;
}

UPDATE

Here is my calling code.

try
{
    int RECEIVED_DATA = await CLIENT_SOCKET.RecvFromSocket(LOCAL_BUFFER.Buffer, LOCAL_BUFFER.Buffer.Length);
    if (RECEIVED_DATA > 0)
    {
        //....
    }
} catch() {}

Here is how CLIENT_SOCKET and BUFFER is declare

public TransferBuffer LOCAL_BUFFER { get; set; }
public TransferBuffer REMOTE_BUFFER { get; set; }
public Socket CLIENT_SOCKET { get; set; }
public InsideConn(Socket _CLIENT_SOCKET)
{
    try
    {
        CLIENT_SOCKET = _CLIENT_SOCKET;
        //...
        LOCAL_BUFFER = new TransferBuffer(8192, 0, 0);
        REMOTE_BUFFER = new TransferBuffer(8192, 0, 0);
        //...
    }
    catch (Exception EX) { AsyncServer.DISCONNECT(CLIENT_SOCKET, MODULE_TYPE); }
}

The problem is, whenever I call above function, I always use try/catch, that means the crash will never happens.

Any idea?

question from:https://stackoverflow.com/questions/65649920/system-objectdisposedexception-net-socket

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...