【问题标题】:Receive a request in c# with asynchronous methods在c#中使用异步方法接收请求
【发布时间】:2012-11-21 20:59:17
【问题描述】:

我正在尝试设置一些网络内容。出于这个原因,我需要通过网络发送和接收数据。特别是 HTTP 消息。我想用 C# 中的异步方法来实现它。当我从网络服务器接收 HTTP 响应时,我想接收数据直到网络服务器关闭连接或(连接:保持活动)直到我收到 HTTP 响应的所有字节。我已经尝试设置此方法但失败了。你能告诉我如何实现这一点。我在 msdn 上看到过这个例子:

    private static void Receive(Socket client) {
    try {
        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = client;

        // Begin receiving the data from the remote device.
        client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReceiveCallback), state);
    } catch (Exception e) {
        Console.WriteLine(e.ToString());
    }
}

private static void ReceiveCallback( IAsyncResult ar ) {
    try {
        // Retrieve the state object and the client socket 
        // from the asynchronous state object.
        StateObject state = (StateObject) ar.AsyncState;
        Socket client = state.workSocket;

        // Read data from the remote device.
        int bytesRead = client.EndReceive(ar);

        if (bytesRead > 0) {
            // There might be more data, so store the data received so far.
        state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));

            // Get the rest of the data.
            client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
                new AsyncCallback(ReceiveCallback), state);
        } else {
            // All the data has arrived; put it in response.
            if (state.sb.Length > 1) {
                response = state.sb.ToString();
            }
            // Signal that all bytes have been received.
            receiveDone.Set();
        }
    } catch (Exception e) {
        Console.WriteLine(e.ToString());
    }
}

使用状态对象:

public class StateObject {
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}

如果我使用与此类似的代码,恐怕会遇到可怕的副作用,因为所有线程都使用相同的状态对象。此外,我必须立即重新发送接收到的数据。

【问题讨论】:

标签: c# http sockets asynchronous


【解决方案1】:

HTTP 不是一个简单的协议。如果您坚持手动执行此操作,暂时不要打扰线程,从单个 HTTP 客户端连接开始,找出正确的消息解析和协议状态机。一旦你开始工作,扩展/扩展/无论如何都会简单得多。

【讨论】:

  • 我知道 HTTP 协议及其工作原理。此外,我知道我必须过滤的 Header-Fields、如何获取消息长度、IP 地址……我已经设计了一个协议状态机。它只是让我生气的线程。单线程程序会很慢。我无法在适当的时间发送和接收消息。这就是为什么我要求一种如何使用异步方法接收消息的好方法。
  • 每次执行new AsyncCallback 时都会创建一个新的状态对象,所以这不是问题。您需要累积读取的字节,直到您可以解析足够多的消息,因此在每次读取后调用您的协议状态机来告诉您是否需要阅读更多内容。
  • 你的意思是:而不是client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);使用 client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), new state(...)); ?虽然已经说过了,但我不确定每次调用回调方法时创建一个新的状态对象是否有效。处理同一个对象并同步处理它的线程不是更好吗?
  • 不,您已经在 Receive() 方法中执行此操作 - 每个套接字一个状态(抱歉,我在之前的评论中措辞不好)。只是不要等到 EOF,尽快解析它。
  • 好的,那么我应该处理同一个对象并同步处理它的线程。当我收到完整的 Header 时(意味着当我收到 /cr/n/cr/n 时)考虑做 Parsing 的事情。你会推荐我使用锁来同步接收吗?
【解决方案2】:

我尝试实现异步接收方法。嗯,还没写完,但基本结构应该很清楚了。

private void BeginReceiveFromWebserver(ClientSocket clientSocket)
    {
        try
        {
            clientSocket.CommunicationSocket.BeginReceive(clientSocket.Buffer, 0, clientSocket.Buffer.Length,
                                                         SocketFlags.None, new AsyncCallback(ReceiveFromWebserverCallback), clientSocket);

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message + "\nfrom Source: " + e.Source + "\nand Stack Trace: " + e.StackTrace);
            //XXX TODO
        }
    }

    private void ReceiveFromWebserverCallback(IAsyncResult ar)
    {            
        ClientSocket clientSocket = ar.AsyncState as ClientSocket;

        try
        {
            int bytesRead = clientSocket.CommunicationSocket.EndReceive(ar);

            if (bytesRead > 0)
            {
                if(!clientSocket.AllBytesReceived)
                {
                    // No locks needed here, because it works like a loop
                    clientSocket.TotalBytesReceived+=bytesRead;
                    clientSocket.BufferUsed++;
                    clientSocket.AddBuffer();
                    clientSocket.SendingToBrowserCompleted.WaitOne();
                    BeginSend(clientSocket, bytesRead);
                    clientSocket.CommunicationSocket.BeginReceive(clientSocket.Buffer, 0, clientSocket.Buffer.Length,
                                                        SocketFlags.None, new AsyncCallback(ReceiveFromWebserverCallback), clientSocket);
                }
                else
                {
                    // all bytes Received
                }
            }
            else
            {
                Console.WriteLine("Webserver Closed Connection");
                //XXX TODO, Webserver closed the Connection
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message + "\nfrom Source: " + e.Source + "\nand Stack Trace: " + e.StackTrace);
            //XXX TODO
        }
    }

收到的每条消息都会重新发送到浏览器。为了确保浏览器以正确的顺序获得这些和平,我在这里设置了 SendingToBrowserCompleted-AutoResetEvent。如果您对更多代码感兴趣,请询问。

【讨论】:

    猜你喜欢
    • 2015-01-26
    • 2012-10-24
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    相关资源
    最近更新 更多