views:

14

answers:

2

I have a webrequest that createst a persistent (keepalive) connection to the server, e.g.:

webRequest.ContentType = "application/x-www-form-urlencoded";
                    // Set the ContentLength property of the WebRequest.
                    webRequest.ContentLength = byteArray.Length;
                    webRequest.Timeout = Timeout.Infinite;
                    webRequest.KeepAlive = true;
                    webRequest.ReadWriteTimeout = Timeout.Infinite;
                    //[System.Threading.Timeout]::Infinite

                    webRequest.UserAgent = "www.socialblazeapp.com";
                    Stream dataStream = webRequest.GetRequestStream();
                    // Write the data to the request stream.
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    // Close the Stream object.
                    dataStream.Close();
                    // Get the response.
                    webResponse = (HttpWebResponse)webRequest.GetResponse();
                    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                    responseStream = new StreamReader(webResponse.GetResponseStream(), encode);
                    while(!responseStream.EndOfStream){ //do something}

I'm wondering why responseStream.EndOfStream becomes true after a while. I would have assumed that because this is a persistent connection, the stream would never close?

Any ideas why this is happening?

+1  A: 

All that is supposed to do is keep the TCP connection open. If I'm reading this correctly, that means you can reuse the same physical TCP connection for multiple requests to a given server. What it won't do is keep your stream open so that the server can send additional information.

If you really want streaming data you should either use a different protocol or straight TCP.

Jonathan Allen
Do you know of any examples that show complete streaming of data?
rksprst
Basically any example on TCP will do, but here is a decent starting point. http://www.codeproject.com/KB/IP/socketsincsharp.aspx
Jonathan Allen
I am using this for Twitter's Streaming API ( http://dev.twitter.com/pages/streaming_api ), just looked at their docs and they want a HTTP connection. Is there any way to make the read stream persistent with an HTTP connection? I just tried getting the response stream again, but it looks like will only work if a new connection is made.
rksprst
Oh, that's an entirely different question. I suggest asking for an .NET example of that API with a footnote mentioning the connection keeps getting closed. Also look at this: http://devblog.yedda.com/index.php/twitter-c-library/
Jonathan Allen
+1  A: 

I think you're confusing keeping the TCP connection open with keeping the response stream open. The TCP connection is the underlying transmission medium, whereas the request and response are individual entities communicated via that connection.

With a persistent connection you [in theory] could issue multiple request/response pairs across the same connection. Without a persistent connection you would essentially open the connection, issue the request, receive the response, then close the connection and then repeat that process for subsequent request/response pairs.

The response itself however is finite in size, once you're received the completed response the stream should close as there is nothing more to tell you. Once you issue another request, another response would follow; I'm not clear as to whether .Net will reuse the underlying persistent connection.

Coding Gorilla
That makes sense, so let's say I want to keep a connection open to a web server for a few hours (with data streaming the whole time), how would I accomplish that?
rksprst
Based on your question here and to the other answer, I think you're misunderstanding the concepts of streaming and connection persistence. Regardless of the connection types, HTTP is a request/response conversation. You will always issue a finite request, and the server will always give you a finite response, which implies there is an end to that response. What you are looking for, as I understand it (and correct me if I'm wrong) is basically a way to have a single response stream variable and just be able to use it over and over. You're not going to be able to do that.
Coding Gorilla
What I'm trying to do is connect to Twitter's Streaming API ( http://dev.twitter.com/pages/streaming_api ) Under the "Connection" section, they have text that implies that you should be able to do this in order to connect to the streaming API via HTTP. If I can't keep a response stream open via HTTP, then I don't get how Twitter would like us to maintain the connection? Or, am I misunderstanding something again?
rksprst
The issue is the layers in between; you're using an HttpWebRequest which is a high level absrtaction, it abstracts the work of opening the TCP connection, sending the request, etc. Their documentation is referring to the raw TCP connection, which using the HttpWebRequest you have no direct control over. If you were to open a TCP connection and manage that yourself, you could indeed do what you're looking to do. But that's a whole different animal than what you're working with now.
Coding Gorilla