Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用BeginReceive时引发的令人困惑的System.SocketException_C#_Sockets - Fatal编程技术网

C# 调用BeginReceive时引发的令人困惑的System.SocketException

C# 调用BeginReceive时引发的令人困惑的System.SocketException,c#,sockets,C#,Sockets,我在(我相信,是对System.Net.Sockets.Socket.BeginReceive的第二次调用)上引发了一个异常(System.SocketException),我不明白为什么会引发该异常。我尝试接收并分别将接收到的数据附加到每个客户端MemoryStream对象。这会导致接收到的事件永远不会被触发,并且一些数据不会附加到流中 static void EndReceive(IAsyncResult ar) { // our client socket

我在(我相信,是对System.Net.Sockets.Socket.BeginReceive的第二次调用)上引发了一个异常(System.SocketException),我不明白为什么会引发该异常。我尝试接收并分别将接收到的数据附加到每个客户端MemoryStream对象。这会导致接收到的事件永远不会被触发,并且一些数据不会附加到流中

    static void EndReceive(IAsyncResult ar)
    {
        // our client socket
        var client = (Client)ar.AsyncState;

        // amount of bytes received in this call (usually 1024 for buffer size)
        var received = client.socket.EndReceive(ar);

        if (received > 0)
        {
            // append to memorystream
            client.stream.Write(client.buffer, 0, received);
        }
        else
        {
            // raise event done
            ClientSent(client, client.stream.ToArray());

            // clear out the memorystream for a new transfer
            client.stream.SetLength(0L);
        }

        try
        {
            // continue receiving
            client.socket.BeginReceive(client.buffer, 0, client.buffer.Length, SocketFlags.None, (AsyncCallback)EndReceive, client);
        }
        catch (SocketException socketException)
        {
            // error thrown every single time:

            // System.Net.Sockets.SocketException: 'A request to send or receive data was disallowed because the socket 
            // is not connected and (when sending on a datagram socket using a sendto call) no address was supplied'
            return;
        }
        catch (ObjectDisposedException disposedException)
        {
            return;
        }
    }

我不知道是什么导致了这个特别的错误。但经过一些修补,我使EndReceive方法正常工作。这是我的新设计:

    static void EndReceive(IAsyncResult ar)
    {
        var client = (Client)ar.AsyncState;
        var received = 0;

        try
        {
            received = client.socket.EndReceive(ar);
        }
        catch (SocketException ex)
        {
            client.socket.BeginDisconnect(false, (AsyncCallback)EndDisconnect, client);
        }

        if (received > 0)
        {
            client.stream.Write(client.buffer, 0, received);

            if (client.socket.Available == 0)
            {
                var sent = client.stream.ToArray();
                client.stream.SetLength(0L);

                Console.WriteLine("Client sent " + sent.Length + " bytes of data");
            }
        }

        try
        {
            client.socket.BeginReceive(client.buffer, 0, client.buffer.Length, SocketFlags.None, (AsyncCallback)EndReceive, client);
        }
        catch (SocketException socketException)
        {
            return;
        }
        catch (ObjectDisposedException disposedException)
        {
            return;
        }
    }

为什么在收到每条消息后要关闭连接。请看msdn示例。示例使用套接字,但您可以使用继承套接字的任何类,如TcpListener或TcpClient:I不关闭连接。您的意思是什么?下面的行是错误的:client.BeginReceive((AsyncCallback)EndReceive,client);请参阅msdn示例:不是,我应该提到我在客户机类中创建了自己的BeginReceive方法,只是为了不必传递缓冲区、偏移量或长度。我没有使用自定义方法更新帖子。我打赌你需要使用此方法。client.socket.BeginReceive(client.buffer,0,client.buffer.Length,SocketFlags.None,(AsyncCallback)EndReceive,/*client*/this);BeginReceive应该有一个新的AsyncCallback并使用ar.AsyncState。去年我试着使用一种单独的方法,就像你尝试使用的那样,但也犯了类似的错误。您正在发送client.buffer,但正在方法顶部获取client。msdn示例表明它是两种状态。