Re: Multiplayer gaming
- From: "Bram" <b.fokke@xxxxxxxxx>
- Date: 21 Jun 2006 08:41:49 -0700
Hi Anuj,
Basically, there are two approaches which could work. Which is the best
depends on the characteristics of the game.
The first approach would be to use plain Tcp sockets. This is very
straightforward:
Server code (not complete, but illustrates the idea):
public class Server {
public void RunServer() {
int port = 19681; // make something up, preferably > 1024
TcpListener listener = new TcpListener(port);
// loop
while(true) {
TcpClient tcpClient = listener.AcceptTcpClient(); // Waits until a
client connects
Client client = new Client(tcpClient.GetStream());
Thread t = new Thread(new MethodInvoker(client.RunClient));
t.Start();
}
}
}
public class Client {
Stream stream;
public Client(Stream s) {
this.stream = s;
}
public void RunClient() {
using(stream) {
// Communicate with client using stream
}
}
}
.... and the client code is even simpler
public void Connect(string hostname) {
TcpClient client = new TcpClient();
client.Connect(hostname, 19681);
using(Stream s = client.GetStream()) {
// communicatie with server using stream
}
}
This method has several advantages:
- It's straightforward easy to set up
- This method allows maximum control over the communication protocol
- Given an efficient protocol this method is fast ensures
However, there are several drawbacks:
- Using streams, it's only possible to communicate using basic data
types. Anything more complicated will require serialization, which .NET
can handle but does complicate matters.
- For anything but the most simple games you will have to implement
some kind of message structure.
That last drawback might not seem like a large problem but consider the
following example:
We have a very simple game with several spaceships which try to kill
each other.
Each spaceship can do the following things:
- Rotate around its axis
- Fire its gun
- Fire its thruster
Furthermore, there are some basic game actions:
- Stop playing
- Send a chat message
To prevent cheating, the game state will be maintained on the server.
The clients simply send the commands. This means that all clients also
have to be updated on the results of each action:
- Ship was damaged
- Ship was killed
- Updated positions of all ships
- Receiving a chat messages
.... etc.
This is a profoundly simple game and yet we have a lot of different
messages we should be able to send. Since we can only use basic data
types, each message might look like this:
- A header, telling what kind of message it is
- Data
- possibly: A footer with a checksum, but we'll omit this for brevity
The code would look something like this:
while(true) {
GameCommand command = ReadCommandFromStream(); // Should be an enum
but you get the idea
byte[] data = ReadDataFromStream();
switch(command) {
case GameCommand.FireGun:
FireGun();
// .....
}
}
Not pretty. And the more complicated the game gets, the worse it'll be.
The second possibility might be more difficult to implement but might
end up being considerably easier to maintain: .NET remoting.
..NET remoting allows a server to expose a .NET object, which can be
instantiated by a client on another computer. The object acts as a
proxy. Method calls are sent to the server and the reply is sent back.
All of this is handled by .NET.
Now the communication could be pretty object-oriented:
class UeberSpaceGameClient : MarshalByRefObject {
public void FireGun() {
}
public class Rotate(float angle) {
}
public void FireThruster() {
}
public void SendChatMessage(string message) {
}
public void StopPlaying() {
}
//... and the messages from the server are events
public event ChatMessageEvent MessageReceived;
public event PositionUpdateEvent PositionUpdated;
public event ShipDamageEvent ShipDamaged;
}
And an added bonus is that it would be quite easy to write anotherFrom a OO perspective this is more cleaner and more straightforward.
client (say, a bot player).
However, there are some drawbacks:
- It's slower than plain sockets. .NET handles a lot of stuff for you
and obviously that will cost you CPU cycles.
- Getting .NET remoting to work is a serious pain in the back. I won't
elaborate, there are numerous examples on the internet but getting from
a basic 'Hello World' to more complicated communication scenario's is
not trivial.
I hope this helps, good luck!
Cheers,
Bram Fokke,
Utrecht, The Netherlands
hisaysanuj@xxxxxxxxx wrote:
Hi,
I am developing a simple multiplayer game in VS 2005 using C#.
Each player would have an individual copy of the game running, which
would connect to a server. The app running on the server would
continuously listen on a specific port for the players and when the two
players are connected, the server would transmit information from one
player to the other.
Can you tell me the .NET classes that would help me here.
Thanks,
Anuj
.
- References:
- Multiplayer gaming
- From: hisaysanuj
- Multiplayer gaming
- Prev by Date: Enable and disable a control in ASP.NET 2.0 using javascript
- Next by Date: Fullscreen drawing and hotkeys
- Previous by thread: Multiplayer gaming
- Next by thread: Enable and disable a control in ASP.NET 2.0 using javascript
- Index(es):
Relevant Pages
|