tags:

views:

1491

answers:

4

I am writing a program. my program receive data from a server through HTTP protocol. the data will be pushed by server to my program. I tried to use WebRequest, but only received one session of data. How can i keep the connection alive, to receive the data from server continuosly, Any help is appreciated.

the following is the SDK document:

Under the authorization of GUEST or ADMIN, it is possible to get the series of live images (Server push). To get the images, send the request to “/liveimg.cgi?serverpush=1” as shown in the Figure. 2-1-1.
When the camera receives the above request from the client, it sends the return as shown in the Figure. 2-2.
Each JPEG data is separated by “--myboundary”, and “image/jpeg” is returned as “Content-Type” header, after “--myboundary”. For “Content-Length” header, it returns the number of bytes in the --myboundary data (excluding “--myboundary”, each header, and \r\n as delimiter). After the “Content-Length” header and “\r\n” (delimiter), the actual data will be sent.
This data transmission will continue until the client stop the connection (disconnect), or some network error occurs.

int len; string uri = @"http://192.168.0.2/liveimg.cgi?serverpush=1";

        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
        req.Credentials = new NetworkCredential("admin", "admin");
        req.KeepAlive = true;

        string line = "";

        HttpWebResponse reply = (HttpWebResponse)req.GetResponse();
        Stream stream = reply.GetResponseStream();

        System.Diagnostics.Debug.WriteLine(reply.ContentType);

        StreamReader reader = new StreamReader(stream);
        do
        {
            line = reader.ReadLine();
            System.Diagnostics.Debug.WriteLine(line);

            System.Threading.Thread.Sleep(300);

        } while (line.Length>0);
A: 

If you are using a standard web server, it will never push anything to you - your client will have to periodically pull from it instead.

To really get server push data you have to build such server yourself.

Otávio Décio
The issue here though, seems to be the server sends you "several" responses. That is, many responses are pipelined(an HTTP/1.1 featore) back to the client.It's very unclear wether HttpWebResponse supporst pipelined reqsponses in any easy way, other than reading the response stream and parse it yourself.But anyway, the HttpWebRequest/HttpWebResponse classes in .NET is a place to start, I guess.
nos
A: 

If I'm understanding you correctly, your server is going to respond to some event by sending data to your client outside of the client making a request/response. Is this correct? If so, I wouldn't recommend trying to keep the connection open unless you have a very small number of clients -- there are a limited number of connections available, so keeping them open may rapidly result in an exception.

Probably the easiest solution would be to have the clients poll periodically for new data. This would allow you to use a simple server and you'd only have to code a thread on the client to request any changes or new work once every minute or thirty seconds or whatever your optimal time period is.

If you truly want to have the server notify the clients proactively, without them polling, then you'll have to do something other than a simple web server -- and you'll also have to code and configure the client to accept incoming requests. This may be difficult if your clients are running behind firewalls and such. If you go this route, WCF is probably your best choice, as it will allow you to configure server and client appropriately.

Ragoczy
+1  A: 

You can keep an HTTP connection open for an extended period of time, if the server supports doing so. (As already mentioned, this will significantly limit the number of simultaneous users you can support.)

The server will need to be set Response.Buffer=false, and have an extended ScriptTimeout (I'm assuming your using ASP.NET on the server side). Once you do that, your page can keep sending Response.Write data as needed until whatever it is doing is done.

Your client will need to process the incoming Response before the connection is complete rather than blocking for the complete response.

richardtallent
+1  A: 

You may want to take a look at StreamHub Push Server - its a popular Comet server and has an .NET Client SDK which allows you to receive real-time push updates in C# (or VB / C++).

rajax