【问题标题】:Connection drop in HttpListener in C# MonoC# Mono 中 HttpListener 中的连接断开
【发布时间】:2014-05-18 09:23:28
【问题描述】:

我有这样的代码

  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。

代码用于从 C++ 客户端连接的 RPC 服务器,我无法更改。客户端希望连接在整个期间保持打开状态,并且在将此服务器与 Mono 一起使用时,在 unix 上出现管道损坏和错误代码 5、eof 在 Windows 上失败。

连接没有问题,但是在第一个命令之后客户端失败。没有引发异常。感谢您的帮助!

编辑 1:我拆开单声道 HttpListener 并直接在我的项目中使用它,现在它在 .Net 上也失败了。代码肯定有问题。附言这次是最新的提交代码。

【问题讨论】:

    标签: c# mono httplistener


    【解决方案1】:

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

    我做错的是我自己处理了 Request.InputStream 流,这是不应该做的。虽然 .Net 对我没有问题,但 Mono 决定检查连接是否可以重用,并且在处理流时失败。

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    相关资源
    最近更新 更多