Game Development Reference
In-Depth Information
Note
Place a try/catch block around the resultTask.get() method to catch any ex-
ceptions that may occur. Windows will throw an exception if the connection fails,
and this is where it will surface.
Communicating the gameplay
Once the player is in the game, we need to communicate the game state to the
other players or the server. The communication of this data is done with a technology
called Sockets . In Windows 8 there are two different types of sockets that define the
protocol type that is used. We'll start by looking at the different options and how to get
started with them before looking at how to read and write data using the DataRead-
er and DataWriter classes.
TCP - StreamSocket
The standard option and the one provided when using the PeerFinder class in
Windows 8 is the StreamSocket . This is a standard socket that creates a TCP con-
nection between the host and client. There are benefits to this option; however, de-
pending on how much data is sent back and forth, or how important you consider
latency, you may want to choose the User Datagram Protocol ( UDP ) instead.
The TCP protocol establishes a connection between a host and client machine to
communicate, and provides one major benefit: reliability. Any messages sent over
a TCP connection have a reasonable guarantee that they will reach their destina-
tion, assuming there isn't any major fault or issue with the connection. This is ac-
complished by tracking the packets that are sent, and resending them as required
to ensure the message arrives in order. That last part there has two important ele-
ments: the packets are resent, and they arrive in order. It wouldn't do to have im-
portant game information go missing (known as packet loss) without you knowing,
or have the latter half of some game data arrive before the first half, without your
knowledge. TCP takes away a lot of the worry when dealing with this as the operat-
ing system handles all of this for you.
Search WWH ::




Custom Search