Game Development Reference
In-Depth Information
object we have one property: Socket . This is our StreamSocket for the connec-
tion, which is ready for use.
If you're looking to connect as a client, you'll need to start with a StreamSocket
and provide an endpoint to connect to, as shown in the code snippet that follows:
auto host = ref new HostName("localhost");
auto port = ref new String(L"6000");
auto socket = ref new StreamSocket();
socket->ConnectAsync(host, port,
SocketProtectionLevel::PlainSocket);
For the endpoint we need to provide the host name of the machine (or the IP ad-
dress) and the port. Once we have that we just need to create a new StreamSocket
and call the ConnectAsync() method, passing in the host and port. The third para-
meter allows you to create an SSL connection; however, further setup is required on
the listener for that, and you don't really need it to connect two computers to play a
game, so we won't look further into it.
UDP - DatagramSocket
The UDP has traditionally been the go-to protocol for game developers, as it
provides the most control and the most performance, at the cost of the nice benefits
mentioned in the TCP section earlier. If you haven't encountered this one before,
UDP is considered a connectionless protocol because it doesn't establish a ded-
icated connection like TCP. Instead, messages (called datagrams ) are transmitted
as required and received whole. That is, unless they don't arrive. One of the primary
downsides to UDP is its unreliability, as packets can be lost, and UDP provides no
guarantee or built-in support for confirmation. Alongside, packets may take different
routes and arrive out of order, and the UDP doesn't assist here either. This means
that you will need to handle all of this yourself, either by enduring the problem (and
working around the packet loss), or writing your own integrity checks to ensure you're
getting the information you need.
The benefits come from the lightweight nature of the protocol. As there is no con-
nection, and packet or order validation, we get a very thin layer that runs as fast as
possible, meaning it is great for the real-time nature games where packet loss may
be tolerated as long as we get a faster system.
Search WWH ::




Custom Search