Connection Drop in HttpListener in C # Mono

I have code like this

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()); } } 

The code works fine on Windows using .Net, but in my testing on Ubuntu 13.04 the client is disabled. I am using Mono 3.2.1.

The code for the RPC server that is associated with the C ++ client, I can not change. The client expects that the connection will remain open for the entire period and will not end with a failed pipe in unix and error code 5, eof on Windows when using this server with Mono.

There is no problem connecting, but after the first command, the client fails. An exception is thrown. Thank you for your help!

EDIT 1: I ripped the mono-HttpListener and used it in my project directly, and now it fails on .Net too. There is definitely something wrong with the code. The postscript this time was the latest commit code.

+7
c # mono
source share
1 answer

My first question, and I myself decided: D

What I did wrong was that I myself managed the Request.InputStream, which should not be executed. While .NET did not have any problems with me, Mono decided to check if the connection could be reused or not, and failed because the stream was deleted.

Thus, the function of utilization of the stream is removed, and it works!

+4
source

All Articles