C#-HTTPListener: Best way to handle idle connections

Started by
4 comments, last by dantz 15 years, 4 months ago
Having a multiple client/server using a C# httplistener/httpwebrequest to transfer message. How should I handle idle connections? Will it be the job of the server or client? Does using a ping mechanism effective? Please suggest others ways.. My idea as of now is this: http://www.koders.com/csharp/fidDA9BB7F0CB74CAC8C021B42DF9EB9DCAFB88845E.aspx?s=httpwebrequest setting the timeout explicitly. TIA.
Advertisement
HTTP typically closes connections after the response is sent, so no need for a timeout under normal conditions. I wouldn't make it any more complex than that unless you have a real need to. So the question then becomes, "what is your definition of idle"? If you want to have your server close a connection that hasn't sent a request quickly enough, fine. If you want to have the client do the same, also fine. You certainly don't need a ping mechanism. That's really the wrong way to look at HTTP.

I don't see the relevance of the code you linked to. That's a unit test for an existing component, not end code you should copy.
With Connection: keep-alive, you can pipeline multiple requests over the same single HTTP connection. However, most servers (and some clients) will close even a keep-alive connection after some number of requests (typically 300 or so), so you'll have to re-open them anyway.
enum Bool { True, False, FileNotFound };
The reason I am using the httplistener is because having an http format may make it easier for me to change the game in to online in the future. How does the online games send and receive messages?

What I am doing now will be using the httplistener in .NET then send the message or commands by a POST method. The command will be in xml format,so I include the message in the body of a POST. Am I doing something that have sense?
Please enlighten me up if I am in a wrong page...

Any comments will be very much appreciated.

Thank you in advance.
Online games usually use their own protocol. However, Flash based games can't really use raw sockets, and instead have to use XML over HTTP for their communications. I don't know if Adobe has improved that in the latest version of Flash, though.

HTTP is a terrible protocol for real-time interaction. If you want to make something like an online real-time RPG, an FPS or a RTS game, you want to use a custom protocol. See the Forum FAQ for various pointers.
enum Bool { True, False, FileNotFound };
Quote:Original post by hplus0603
Online games usually use their own protocol. However, Flash based games can't really use raw sockets, and instead have to use XML over HTTP for their communications. I don't know if Adobe has improved that in the latest version of Flash, though.

HTTP is a terrible protocol for real-time interaction. If you want to make something like an online real-time RPG, an FPS or a RTS game, you want to use a custom protocol. See the Forum FAQ for various pointers.


Thank you for your suggestion.
I will try to learn those or maybe I should use TCP as of the moment.

This topic is closed to new replies.

Advertisement