在此套接字异步接收方法中获取堆栈溢出异常

时间:2013-07-03 15:50:51

标签: c# sockets udp

我有一个客户在下面的方法中报告堆栈溢出异常(在UDP套接字上)。文档说Socket.RecieveFromAsync可以同步返回,但我无法弄清楚如何在单元测试中做到这一点。为了测试目的,我如何强制该方法同步返回?

private void OnReceive(object sender, SocketAsyncEventArgs e)
{
    if (e != null)
    {
        var length = e.BytesTransferred;
        if (length > 0)
        {
            FireBytesReceivedFrom(datagram, length, (IPEndPoint) e.RemoteEndPoint);
        }
        e.Dispose();
    }
    var s = _socket;
    if (s != null)
    {
        var args = new SocketAsyncEventArgs();
        try
        {
            args.RemoteEndPoint = _remoteEndPoint;
            args.SetBuffer(datagram, 0, datagram.Length);
            args.Completed += OnReceive;
            if (!s.ReceiveFromAsync(args))
                OnReceive(this, args); // stack overflow occurs here
        }
        catch (ObjectDisposedException)
        {
            // this is expected after a disconnect
            args.Dispose();
        }
    }
}

0 个答案:

没有答案