I am writing a game server for a turn-based game in Java. These are the facts:
First of all, I excluded UDP from my option list as I need a reliable protocol because in rare situations I really need to send some data which cannot fit in one packet and I don't want to bother with merging packets and similar things, tracking the order of the arrived packages and other low-level stuff.
So the dilemma is whether to use TCP or HTTP.
TCP attempt #1
The connection from the client to the server (and vice-versa) is opened all the time. This way, when a player makes a move, the server can easily notify other players in the game which move was made. The main thing which bothers me with this approach is whether it is advisable or even possible to have as much as 1000 connections and sockets opened all the time?
TCP attempt #2
The alternative which I thought of is, to use to establish a seperate connection/socket on every request from a client. A client would open a connection, send some small data to the server and close that connection. With this approach, I can have a fixed size thread pool of let's say 10 and process client's requests in each thread separately so that there is at most 10 connectinos/sockets opened at any time. But there are two things which bothers me with this approach:
What is the cost of establishing a TCP socket/connection? Is this an expensive operation or this is done in only a few ms (or less)?
HTTP
I am opened to suggestions/advice of any kind.
The two most common protocols are TCP (transmission control protocol) and UDP (user datagram protocol).
UDP is less reliable than TCP, but is much simpler. UDP is used for situations where some data loss is acceptable, like live video/audio, or where speed is a critical factor like online gaming. While UDP is similar to TCP in that it's used to send and receive data online, there are a couple of key differences.
Pretty much every game will use some TCP, as HTTP over TCP is the easiest way to download updates and do session discovery to set up the UDP channels. But, many games that want low latency will use UDP for that, often peer-to-peer as well.
Most people say UDP is always better for real-time games than TCP. My understanding is that TCP tries to re-send packets over and over til the other side gets them whereas UDP doesn't care. Most of the things I've read is that UDP is a must for any realtime game and TCP is terrible.
Just for your information: HTTP is TCP. A specific protocol that uses TCP, that is. HTTP is based upon TCP, just like TCP is based on IP, etc. So really your choice is between HTTP over TCP or a custom protocol over TCP. You're right that UDP is a poor match here.
If you're writing the server yourself, many of the benefits of using HTTP go away. HTTP's main benefit is that there are highly optimised servers already available so you can use it as a simple and effective RPC system. But if you're writing the server yourself you're not likely to reach the efficiency of the likes of Apache so you have to ask why you wouldn't just pick a simpler protocol to use? Besides, hacking around the pull-only nature of HTTP would seem to be the wrong way to go.
With this in mind, I'd just use a more lightweight protocol over TCP. You get more control over the connections and can notify interested clients of updates without requiring them to poll for changes. You also are able to lose the HTTP request/response overhead which is mostly superfluous in your case. You can instead use a fairly simple bespoke protocol, perhaps based around XML or JSON, or maybe one of the existing RPC methods available.
I see that you look at the very "low level". Have you tried to use something at the higher level, something like http://code.google.com/p/kryonet/ (also developed for games) ? (and found maybe bad performance? )
I think the results delivered by KryoNet are quite good and it's very fast to program with their API.
A server is supposed to be able to have around 20'000 sockets open at the same time. If you decide to use http, you can use the new comet features of tomcat 6+ or jetty 6+, otherwise a thread will be allocated to each request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With