How can I make a peertopeer multiplayer game?

http://gamedev.stackexchange.com/questions/3887/how-can-i-make-a-peer-to-peer-multiplayer-game

————————————————————————————————————————————————————————————————————————

How can I make a p2p multiplayer game? I would like to have a server-less multiplayer game. But then, how all the clients know each other?

Why the p2p-protocol is so famous in file transfer but not in multiplayer games?

——————————————————————————————————————————

Peer to peer games generally still have a game host. Its the game host that posts the game to the master games list and accepts new connections. Whenever the game host accepts a new client to the game it notifies all existing clients about the new client so that they can ensure they connect to the new client.

The simplest way to implement p2p is with a lobby. All clients connect to the host in a lobby (or chat room). When the host is ready the player presses start and they all enter the game at the same time (commonly used in strategy games). A more complex approach is to use "drop-in drop-out" where players can join and leave mid game, however this is a lot more complex to implement in a p2p game and requires a feature called host-migration.

A good number of games use peer to peer networking, including most strategy, sports and driving titles. Just about all Xbox360 and PS3 games use p2p networking. The client-server architecture is mostly used in first person shooter or MMO games.

Client-Server is generally easier to implement as only 1 machine has not know the entire game state, the clients are basically just renderers with some prediction to make things look smooth.

When you build a p2p engine, all clients need a full state of the game world and they all are required to stay in sync.

For more details on p2p and client-server architectures I suggest you read the following article: http://gafferongames.com/networking-for-game-programmers/what-every-programmer-needs-to-know-about-game-networking/

And if you're new to networking in general checkout the other great articles on that site. Glenn is a networking genius.

————————————————————————————————————————————————

There are many reasons p2p is not popular in games, mostly due to lag. Everyone is as slow as the slowest player. We're not talking about bandwidth here, but ping time.

p2p can transfer tons of data, but it does so with a quite high ping, games need to transmit very small amounts of data, with minimal ping time.

————————————————————————————————————————————————

A good example of 'true peer-to-peer' gameplay would be a real-time-strategy game such as Starcraft.

In a game with hundreds of units/projectiles in motion, it's not practical to repeatedly send unit positions/states over the network to all other players, so one solution here is for all players to run the (exact same) simulation in sync.

When one player performs an action, the command/order ('move zergling to X,Y') can be sent to all other players, to be executed by all instances of the simulation a fraction of a second later.

In this situation, if any player disconnects, the game can continue - as there's no need for a server/host to be running the game, the remaining players can carry on.

However, keeping the games in sync is non-trivial, you have to use a fixed timestep for the game logic updates, and must be very careful with use and seeding of random number generators, to ensure that the simulations will not diverge!

————————————————————————————————————————————————

It would be a bit disingenuous to claim that it's not famous for games when most Real Time Strategy games (Star Craft series, Command and Conquer Series) and many FPS games (Call of Duty: Modern Warfare 2) use it.

That said how one learns about the game is up to the matchmaking / lobbying service that you use or create. But even once one learns about the game, there still may be one or more peers that are more equal than others. Consider the case of 3 clients wishing to play, one behind an open nat, 2 behind strict (Closed) nats. The open nat peer can take connections from the other two. But the 2 strict can not connect directly to each other, they will require the open nat to relay the packets. If the open nat peer drops from the game, then either another relay would need to be found or the game will be disrupted.

————————————————————————————————————————————————————————————————————————————

同步模拟

——————————————————————————————————
傲轩游戏网
原文地址:https://www.cnblogs.com/cuizhf/p/3054560.html