Socket.EndReceive(...)没有正确返回

时间:2012-12-17 05:37:54

标签: c# .net exception-handling

我正在尝试使用Socket.BeginReceive(...)和Socket.EndReceive(...)作为我的UDP数据包接收器客户端。它按预期接收和处理数据包,但是,当我希望取消并关闭套接字时,我实现的Close()函数退出Socket.EndReceive(...)。我假设在线程上抛出异常,但我无法弄清楚如何捕获异常以查看问题所在。之前我使用过Socket.EndReceive(...),并将SocketError作为Success返回。这是一些代码,显示我如何使用套接字。

更新代码

void _startReceiving()
{
    _buffer = new byte[Marshal.SizeOf(typeof(EthernetShare.Message))];
    _receiveResult = _udpSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, messageProcessor, null);
}

private void messageProcessor(IAsyncResult result)
{
    int packetSize = _udpSocket.EndReceive(result);
    if (packetSize == _buffer.Length)
    {
        byte[] packet = _buffer;
        IAsyncResult asyncResult = result;
        _startReceiving();
        OnMessageReceieved(_buffer.ToStruct<DataStreamingMessage>());
    }
}
public void Stop()
{

    _continue = false;
    SocketError error;
    try
    {
        int tmp = _udpSocket.EndReceive(_receiveResult, out error);

    }
    catch (Exception)
    {

        throw;
    }
    _udpSocket.Close();
}

旧代码

private Socket _udpSocket;
private byte[] _buffer;
private IAsyncResult _receiveResult;
void _startReceiving()
{
    byte[] buffer = new byte[Marshal.SizeOf(typeof(EthernetShare.Message))];
    _receiveResult = _udpSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, messageProcessor, null);
    //_receiveResult = _udpSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, messageProcessor, _continue);
}

private void messageProcessor(IAsyncResult result)
{

    //if ((bool)result.AsyncState && result.IsCompleted)
    if (result.IsCompleted)
    {
        _buffer = new byte[Marshal.SizeOf(typeof (EthernetShare.Message))];
        //_receiveResult = _udpSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, messageProcessor, _continue);
        _receiveResult = _udpSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, messageProcessor, null);
    }
}


public void Stop()
{

    _continue = false;
    SocketError error;
    try
    {
        int tmp = _udpSocket.EndReceive(_receiveResult, out error);

    }
    catch (Exception)
    {

        throw;
    }
    _udpSocket.Close();
}

1 个答案:

答案 0 :(得分:4)

您使用APM(异步编程模型)是错误的。请参阅:http://msdn.microsoft.com/en-us/library/ms228963.aspx

每次对BeginXXX的调用都应与EndXXX匹配。

在伪代码中,它可能看起来像这样:

private bool isClosed;
private Socket socket;

void BeginReceiveNextPacket(){
    socket.BeginReceive(..., EndReceiveNextPacket);
}

void EndReceiveNextPacket(IAsyncResult result){
    try{
        // By making a call to EndReceive, we allow the socket to wrap up, close internal handles and raise any exceptions if they exist.
        socket.EndReceive(result); 

        // Now make a new call to BeginReceive after we invoked the actual call to EndReceive.
        BeginReceiveNextPacket();
    }
    catch(SocketClosedException) {
        if (closed){
            // We forcefully closed this socket. Therefore, this exception was expected and we can ignore it.
        }
        else{
            throw; // Catch an unexpected exception.
        }
    }
}

void Stop(){
    isClosed = true;
    socekt.Close();
}