c# Mono中HttpListener的连接丢失

本文关键字:连接 HttpListener Mono | 更新日期: 2023-09-27 18:03:57

我有一个这样的代码

  public async void Start()
   {
       Logger.Log(Logger.LogLevel.General, "Beginning Listen!");
       HttpListener listener = new HttpListener();
       listener.Prefixes.Add(Statics.Config.IniReadValue("http-server"));
       listener.Start();
       while (true)
       {
           HttpListenerContext client = await listener.GetContextAsync();
           AcceptClient(client);
       }   
   }
   public async void AcceptClient(HttpListenerContext client)
    {
        try
        {
            string sRequest = Helpers.GetRequestBody(client.Request);
            if (sRequest == "")
                return;
            client.Response.ContentType = "application/json";
            //Do a bunch of stuff here
            string s = JsonConvert.SerializeObject(response);
            byte[] byteArray = Encoding.UTF8.GetBytes(s);
            client.Response.ContentLength64 = byteArray.Length;
            client.Response.OutputStream.Write(byteArray, 0, byteArray.Length);
            client.Response.OutputStream.Close();
            client.Response.Close();
        }
        catch (Exception e)
        {
            Logger.Log(Logger.LogLevel.Error, e.ToString());
        }
    }

代码在使用。net的Windows上工作得很好,但在我对Ubuntu 13.04的测试中,客户端被删除了。我正在使用Mono 3.2.1.

代码是一个RPC服务器连接到一个c++客户端,我不能改变。客户端期望连接在整个期间保持打开状态,但在unix上出现管道破裂,在Windows上错误代码为5,eof,当使用Mono服务器时。

在连接上没有问题,但是在第一个命令之后,客户端失败了。没有提出任何例外。谢谢你的帮助!

编辑1:我撕开了mono HttpListener,并直接在我的项目中使用它,现在它在。net上也失败了。代码肯定有问题。注:这次是最新的提交代码。

c# Mono中HttpListener的连接丢失

我自己解决了第一个问题:D

我做错的是我正在处理请求。InputStream流我自己,不应该这样做。虽然。net对我这样做没有任何问题,但Mono决定检查连接是否可以重用,并在处理流时失败。

所以删除了处理流的函数,它工作了!