Chapter 13. Networking for iPhone Games: Introduction

Up until now, you have been learning about how to make iPhone games that look, sound, and feel great. In this and the next three chapters, we are going to make them even more engaging and fun. I am going to show you how to use the power of the iPhone SDK to turn your games from a solitary experience into something that will bring your players and their iPhones and iPod touches together.

In this chapter, we'll start with a quick overview of the frameworks and technologies that are involved in making applications talk to each other.

In Chapter 14, we'll build our first multiplayer game using the GameKit framework. In it, two players will go head-to-head over Bluetooth in real time in a remake of the classic game Pong.

Then, after whetting your appetite for connectivity, Chapter 15 takes a look at more sophisticated networking APIs, such as Bonjour and CFNetwork. We'll create a game that will let you invite all of your friends and family to participate in a math puzzle competition on your local wireless network or over Bluetooth.

Finally, if you decide to build games that transcend local networks and connect players from around the world over the Internet, Chapter 16 will give you some pointers about how to go about doing that.

And with that, it's time to meet the network.

Meet the Network

Besides having support for multitouch input, hardware-accelerated 3D and 2D graphics, and a robust audio toolkit, both the iPhone and iPod touch come equipped with excellent communication capabilities. Having all of those things together in one device makes for an ideal platform for creating multiplayer games. Before we dive into demonstrating how to actually make those, let's look at the technologies we'll be dealing with along the way.

Network Interfaces

iPhones and iPod touches have three ways to send and receive data, all of them utilizing radio frequencies:

  • Wi-Fi (802.11 wireless networking standard): As of the writing of this book, all of the models of both devices can use this technology, giving them access to other devices on the same wireless networks and Internet connectivity via access points.

  • Bluetooth: This is a wireless protocol for exchanging data between devices over short distances. Even though Bluetooth support was included in the original iPhone, only starting with iPhone 3G and iPod touch second generation can Bluetooth be used to establish "personal area networks," allowing devices to connect to each other and exchange data using TCP/IP.

  • Cellular data network: By using a mobile phone service provider's infrastructure, iPhones are capable of connecting to the Internet using technologies such as GSM with EDGE, 3G, and so on.

These communication links are usually called network interfaces.

TCP/IP

When it comes to delivering information over network interfaces, the iPhone operating system relies on a set of rules for formatting, transmitting, and routing data known as TCP/IP, which stands for Transmission Control Protocol/Internet Protocol. Vast majority of computer networks in the world today use TCP/IP to ensure fast, safe, and reliable data delivery, including the biggest network of them all—the Internet.

Under the hood, TCP and IP are two different protocols that work together closely, but have different responsibilities. TCP ensures that packets of data are transmitted from one end to another in an orderly manner and without losses. IP, on the other hand, is concerned only with how to address and deliver each parcel of data. There are two major versions of IP: IPv4 and IPv6, with the former being more widespread as of the writing of this book.

In order to be able to specify where a particular transmission is going to or coming from, each device that uses TCP/IP networking must be assigned an IP address, which uniquely identifies the device on a particular network. In IPv4, IP addresses consist of four numbers, between 0 and 255, separated by dots (e.g., 192.168.1.56). It is possible for one device to be connected to several networks at once using different network interfaces, in which case it will have several different IP addresses. An example of such a configuration is shown in Figure 13-1.

An iPhone 3G can have three IP addresses assigned to it at the same time if each network interface is being used.

Figure 13.1. An iPhone 3G can have three IP addresses assigned to it at the same time if each network interface is being used.

One device can have several simultaneous but separate data exchanges happening over the same network. To be able to distinguish between those, TCP/IP has the notion of a port, which is identified by a number, from 0 to 65535. For example, whenever you visit a web site, by default, your browser knows to connect to port 80 on the web server, because that particular port number is reserved for data exchanges related to web content.

As a real-world analogy, think of post offices and mail delivery. Apartment buildings have street addresses, which correspond to devices having IP addresses. But that level of granularity might not be enough to deliver mail from one residence to another if we are talking about buildings with dozens or hundreds of apartments. Therefore, each apartment in a particular building has a number, which is analogous to port number in TCP/IP. Instead of piling all of the mail destined to a particular building in its lobby, envelopes and packages are distributed to mailboxes that belong to individual apartments, a system that makes for much more orderly delivery.

TCP/IP is actually a family of protocols, each of which plays a different role. We will be dealing with only a couple of them in the chapters to come. Depending on your choice of protocol, data delivery can be reliable but slower (TCP) or unreliable but faster (User Datagram Protocol, abbreviated to UDP). We will cover the differences between these two protocols in a bit more detail in Chapter 16.

Bonjour

Built on top of TCP/IP, Bonjour is a protocol that allows devices and applications to find each other on the network using names that map to IP addresses and port numbers. Here are some of the places where you might have seen this protocol in action:

  • When you connect to a Wi-Fi network with other computers on it, names of some of those computers show up in your iTunes, and you are able to browse their libraries and play some of their music.

  • When a friend comes over to your house and connects his laptop to your network, his computer's name suddenly shows up in your Finder, allowing you to drag and drop files in and out of some folders on his computer.

  • When you need to connect to a remote printer on your company's network, its name magically appears without you needing to remember its IP address.

Bonjour treats all of these instances of iTunes, file-sharing destinations, and printers as services that have human-readable names and types. Instead of someone maintaining a centralized directory of services on a particular network, each service constantly advertises its existence on that network. Applications that want to use services of a particular type can listen for such advertisements, and discover the IP address and port for contacting each particular service.

iPhone SDK and Networking

TCP/IP and Bonjour are fairly complex protocols. However, as you have probably come to expect by now, the iPhone SDK comes with several tools that allow us to take advantage of the device's communication capabilities without needing to get our hands too dirty.

Sockets and Connections

When talking about sources or destinations of messages that get sent over a TCP/IP network, we will refer to sockets. A socket is an endpoint on the network, which represents a unique combination of IP address, port number, and transmission protocol (UDP or TCP). In iPhone OS, as in many other systems, sockets are resources that are managed by the operating system.

A connection is a logical link between two sockets (usually residing on different devices) that represents a separate data-exchange session.

BSD Socket API

Also known as Berkeley sockets, the BSD Socket API is the de facto standard when it comes to networking libraries for the C programming language. It gets its name from the version of Unix that first introduced this API: Berkeley Software Distribution. Today, you can find an implementation of this framework for nearly every operating system that has a C-based software development toolkit.

In iPhone SDK, Berkeley sockets is the lowest level networking API that developers can access. We won't be using it directly in this book (except for a couple of functions that aren't available otherwise). Rather, we will focus on more developer-friendly frameworks that Apple introduced in Mac and iPhone operating systems.

CFNetwork

CFNetwork framework is a nice Objective-C wrapper around Berkeley sockets that makes it easier to write networking code that plays nicely with other frameworks in the iPhone SDK. It provides developers with an implementation of the HTTP and FTP protocols, and eases the task of handling various TCP/IP network events by integrating sockets with run loops. You'll learn more about this framework in Chapter 15.

NSNetServices

NSNetServices is a group of classes that allow you to use Bonjour protocol to advertise and discover services. They are also covered in Chapter 15.

GameKit

In an attempt to advance multiplayer gaming, Apple decided to put together a framework that includes several tools that make the process of creating those kinds of games easier. The following are the two major features of this framework:

  • Peer-to-peer networking over Bluetooth: This will come in handy when we are building a simple two-player game in Chapter 14.

  • Support for voice chat between two players: Even though this feature isn't very useful in situations where the two participants are located right next to each other (connected via Bluetooth or a local wireless network), it can be a great capability to add to a game that is played over the Internet. We will talk about this feature briefly in Chapter 16.

In GameKit terminology, peer means "someone or something that we are exchanging data with." With a gaming twist, it can refer to an opponent or another player. We will also be talking about sessions. A session is a data exchange between two peers that gets established, goes on for some time, and then is terminated. Peer and session are similar to the notions of socket and connection.

Note

GameKit uses TCP/IP to establish sessions and exchange data between peers, which theoretically means that you should be able to use any of the available network interfaces to communicate. In practice, the picture is a bit more complicated. iPhone OS 3.1 and higher can utilize both wireless (in some cases) and Bluetooth network interfaces to establish peer-to-peer connections, but iPhone OS 3.0 is limited to Bluetooth only. At the same time, the iPhone simulator also has support for GameKit peer-to-peer networking, but only via your computer's wireless or Ethernet connection, not via Bluetooth. This means that in order to test your GameKit-enabled game, you need to carefully consider which version of the iPhone OS is running and which communication channels to employ on each device that you are planning to use.

Summary

This chapter was meant to prepare you for what's to come in the next ones. So far, we have covered some of the basics of TCP/IP networks and briefly looked at the frameworks and libraries we will be dealing with in the next three chapters. By now, you should be hungry to see some code and to write some more games. Fire up your Xcode. It's time to play!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset