我正在尝试设置一些网络资料。出于这个原因,我需要通过网络发送和接收数据。特别是HTTP消息。我想用C#中的异步方法实现它。当我从Web服务器接收HTTP响应时,我希望在Web服务器关闭连接或(连接:保持活动)之前接收数据,直到我收到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());
}
}
使用State Object:
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();
}
如果我会使用类似的代码,我害怕遇到可怕的副作用,因为所有的线程都使用相同的状态对象。此外,我必须立即重新发送收到的数据。
答案 0 :(得分:0)
HTTP不是一个简单的协议。如果您坚持手动执行此操作,请不要打扰线程,从单个HTTP客户端连接开始,找出正确的消息解析和 协议状态机 。一旦你开始工作,它就可以更容易扩展/扩展/无论如何。
答案 1 :(得分:0)
我试图实现异步Receive方法。好吧,它尚未完成,但基本结构应该清楚。
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。如果您对更多代码感兴趣,请询问。