我试图实现多线程服务器。很少有客户端(< 10)会向服务器发送一些数据(PUT请求)。在GET服务器上,必须用这些数据发送集合。以下是我的服务器代码:
class Server
{
private HttpListener httpListener;
private static System.Threading.AutoResetEvent listenForNextRequest = new System.Threading.AutoResetEvent(false);
public static BlockingCollection<NodeInfo> locations = new BlockingCollection<NodeInfo>();
public Server()
{
const string ip = "127.0.0.1";
const int port = 9999;
string prefix = String.Format("http://{0}:{1}/", ip, port.ToString());
httpListener = new HttpListener();
httpListener.Prefixes.Clear();
httpListener.Prefixes.Add(prefix);
Console.WriteLine("HTTP server is running ...");
Console.WriteLine("Listening on {0} ...\n", prefix);
}
public void Start()
{
httpListener.Start();
ThreadPool.SetMaxThreads(10, 10);
ThreadPool.QueueUserWorkItem(new WaitCallback(Listen));
Thread.Sleep(7000);
}
internal void Stop()
{
httpListener.Stop();
}
private void Listen(object state)
{
while (httpListener.IsListening)
{
httpListener.BeginGetContext(new AsyncCallback(ListenerCallback), httpListener);
listenForNextRequest.WaitOne();
}
}
private static void ListenerCallback(IAsyncResult result)
{
HttpListener listener = (HttpListener)result.AsyncState;
// Request
HttpListenerContext context = listener.EndGetContext(result);
if (context.Request.HttpMethod.Equals("PUT"))
{
PutRequestProcess(context);
}
if (context.Request.HttpMethod.Equals("GET"))
{
GetRequestProcess(context);
}
// Process next request
result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
}
private static void PutRequestProcess(HttpListenerContext context)
{
// Request object
HttpListenerRequest request = context.Request;
using (Stream stream = request.InputStream)
{
// Deserialize
DataContractSerializer ser = new DataContractSerializer(typeof(NodeInfo));
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
// Deserialize the data and read it from the instance.
NodeInfo nodeInfo = (NodeInfo)ser.ReadObject(reader, true);
reader.Close();
stream.Close();
locations.Add(nodeInfo);
}
// Response object
HttpListenerResponse response = context.Response;
response.StatusCode = (int)HttpStatusCode.OK;
using (Stream stream = response.OutputStream)
{
stream.Close();
}
}
private static void GetRequestProcess(HttpListenerContext context)
{
// Response object
HttpListenerResponse response = context.Response;
response.StatusCode = (int)HttpStatusCode.OK;
using (Stream stream = response.OutputStream)
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream))
{
DataContractSerializer ser = new DataContractSerializer(typeof(List<NodeInfo>));
{
writer.WriteStartDocument();
ser.WriteObject(writer, locations.ToList());
}
stream.Close();
}
}
}PUT请求似乎正在工作。但是,当我尝试接收这个集合(GET请求)时,反序列化有一个异常:调试器中的Unexpected end of file...,并不是XML字符串末尾的所有标记。我认为写线程(服务器)没有完成。我怎么才能修好它?我也没什么问题。这个实现线程安全吗?如果我使用并发集合,我不需要使用信号量或其他同步?
非常感谢!
发布于 2014-11-15 23:38:50
看起来,您正在过早地关闭输出流。在GetRequestProcess()中,只需删除对stream.Close()的调用即可。当它关闭时,它会让作者冲到溪流上。只要正确地处理对象,就不需要显式地关闭任何东西(就像在这里一样)。
至于你的其他问题,我建议你发布一个不同的问题,如果你有具体的问题,让线程安全的工作,没有所有的I/O代码来混淆事情。如果只希望有人检查您的代码,请使用堆栈Exchange上的“代码评审站点”。
https://stackoverflow.com/questions/26952007
复制相似问题