views:

856

answers:

2

HttpListener gives you response stream, but calling flush means nothing (and from sources it's clear, because it's actually doing nothing). Digging inside HTTP API shows that this is a limitation of HttpListener itself.

Anyone knows exactly how to flush response stream of HttpListener (may be with reflection or additional P/Invokes)?

Update: You can't http stream anything if you don't have a flush option or ability to define buffer size.

A: 

Flush only works in most of the System.Net namespace when Transfer-Encoding is set to Chuncked, else the whole request is returned and Flush really does nothing. At least this is what I have experienced while working with HttpWebResponse.

Nick Berardi
Question is not WHY it's not working... But imagine http streaming where it's important to send something as soon as possible. Chunked data is not an option here at all. HttpListener actually sending something BEFORE you've finished request, but it's using pretty large unconfigurable buffer.
Mash
And btw, ASP.NET flushes ok when you're asking it... ASP.NET requires some hacks to obtain InputStream content before full POST body has been retrieved, but OutputStream works ok.
Mash
HTTP is not really a streaming protocol. It is meant to transmit text over the internet, thus the name hyper-text transmition protocol. If you want streaming I would suggest a socket. Or something a little better at streaming content in small enough packets for your to control.
Nick Berardi
Thank you Nick, but you'd be amazed that most of the streaming traffic today uses HTTP. HTTP is nothing more than just HTTP header in front of binary TCP stream... The only question is managed tools to make a server. HttpListener is fastest managed option for TCP based servers, but MS forgot to add manual flushing in it.
Mash
Is there any reason you are tied to the HttpListener? Wouldn't a URL rewriter with a proxy accomplish the same thing? http://www.coderjournal.com/2009/04/managed-fusion-url-rewriter-was-featured-at-pdc-2008/
Nick Berardi
A: 

I've not tried this yet, but how about writing a separate TCP server for streaming responses? Then forward the request from the HttpListener to the "internal" tcp server. Using this redirect you might be able to stream data back as you need.

As for flushing it, they only way I see to do it is to simulate a dispose, without actually disposing. If you can hack into the HttpResponseStream object, tell it to dispose, unset the m_Closed flag, etc, you might be able to flush the streaming data.

Erich Mirabal
Writing separate TCP server? It's better just to write everything with TcpListener (which is faster than .NET Sockets), but it's much slower than HttpListener. I'll check sources of dispose, but imho that's not the right way. Whether to flush or not in http.sys defined with the data packet itself.
Mash