我在C#和.NET游戏中相当新鲜,所以我在网上发现了一些练习。更具体地说,是异步Web服务器。我目前遇到一些问题,让回调发生。 Socket.BeginReceive
第一次触发它的回调,但当我尝试以递归方式(如MSDN教程中所示 - 获取任何剩余数据)时,它永远不会触发。我目前正在使用.NET 4.0和VS2010。
这是我的代码:
HTTPServer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Collections;
namespace UPnPServer.net.HTTPServer
{
public class HTTPServer
{
private TcpListener tcpListener;
private Thread listenerThread;
private ManualResetEvent allDone;
private Boolean kill = false;
private ArrayList requestHandlers;
public HTTPServer(int port)
{
this.allDone = new ManualResetEvent(true);
this.tcpListener = new TcpListener(IPAddress.Any, port);
this.listenerThread = new Thread(new ThreadStart(Listen));
this.requestHandlers = new ArrayList();
this.listenerThread.Start();
}
private void Listen()
{
this.tcpListener.Start();
while (!this.kill)
{
this.allDone.Reset();
tcpListener.BeginAcceptSocket(new AsyncCallback(AcceptCallback), null);
this.allDone.WaitOne();
}
this.tcpListener.Stop();
}
private void AcceptCallback(IAsyncResult ar)
{
Socket client = tcpListener.EndAcceptSocket(ar);
HTTPRequestHandler handler = new HTTPRequestHandler(client);
handler.RequestProcessed += new HTTPRequestEventHandler(RequestProcessed);
this.requestHandlers.Add(handler);
this.allDone.Set();
}
private void RequestProcessed(HTTPRequestHandler sender)
{
System.Diagnostics.Trace.WriteLine("Ferdig.");
this.requestHandlers.Remove(sender);
}
}
}
HTTPRequestHandler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
namespace UPnPServer.net.HTTPServer
{
public delegate void HTTPRequestEventHandler(HTTPRequestHandler sender);
public class StateObject
{
public Socket Socket = null;
public const int BufferSize = 4096;
public byte[] Buffer = new byte[BufferSize];
public StringBuilder SB = new StringBuilder();
}
public class HTTPRequestHandler
{
public event HTTPRequestEventHandler RequestProcessed;
private Socket socket = null;
private const int bufferSize = 1024;
private byte[] buffer = new byte[bufferSize];
private StringBuilder sb = new StringBuilder();
private void OnRequestProcessed()
{
if (this.RequestProcessed != null)
{
this.RequestProcessed(this);
}
}
public HTTPRequestHandler(Socket client)
{
System.Diagnostics.Trace.WriteLine("Accepted...");
this.socket = client;
StateObject state = new StateObject();
state.Socket = client;
this.socket.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
private void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
System.Diagnostics.Trace.WriteLine("Hmm...");
int read = 0;
read = this.socket.EndReceive(ar);
System.Diagnostics.Trace.WriteLine(read);
if (read > 0)
{
this.sb.Append(Encoding.ASCII.GetString(this.buffer, 0, read));
this.socket.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
return;
}
else
{
if (this.sb.Length > 1)
{
String request = this.sb.ToString();
this.ProcessRequest(request);
}
else
{
}
}
}
public void ProcessRequest(String request)
{
System.Diagnostics.Trace.WriteLine(request);
this.socket.Close();
this.OnRequestProcessed();
}
}
}
请帮帮我,因为我快要疯了。感谢。