1

我正在尝试设置一些网络内容。出于这个原因,我需要通过网络发送和接收数据。特别是 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();
}

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

4

2 回答 2

0

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

于 2012-11-19T22:08:46.353 回答
0

我试图实现异步接收方法。好吧,它还没有完成,但基本结构应该清楚了。

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。如果您对更多代码感兴趣,请询问。

于 2012-11-21T12:59:17.287 回答