with any other devices (it can be used internally, on a single machine only). Addresses that begin with 10 or 192 are not routable, meaning they can communicate with other devices on the same local network segment but cannot connect to other segments. Every address on a particular network segment must be unique or collisions may occur and it gets ugly.
The routing of packets on an IP network—how packets traverse the network and go from one segment to another—is handled by routers. Routers speak to each other using IP addresses and other IP-related information.
TCP AND UDP
TCP and UDP are different types of delivery protocols that are commonly used with TCP/ IP. TCP is reliable, and UDP is fire and forget. What does this mean? It means that TCP includes extra data to guarantee the order of packets and to send back an acknowledg- ment once a packet is received (the common analogy is certified mail: the sender gets a receipt that shows the letter was delivered and signed for and therefore knows the recipient got the message). UDP, on the other hand, doesn’t provide any ordering or acknowledgment (it’s more like a regular letter: it’s cheaper and faster to send, but you basically hope the recipient gets it—you don’t know for sure).
APPLICATION PROTOCOLS
Once a packet is sent and delivered, an application takes over. To send an email mes- sage, for example, SMTP defines a rigorous set of procedures that have to take place. You have to say hello in a particular way and introduce yourself; then you have to sup- ply from and to information, followed by a message body in a particular format. Simi- larly, HTTP defines the set of rules for the internet—which methods are allowed (GET,
POST, PUT, DELETE) and how the overall request/response system works between a cli- ent and a server.
When working with Android, and Java-related APIs in general, you won’t typically need to delve into the details of any of the lower-level protocols, but you may need to know the major differences we have outlined here for troubleshooting, and you will need to be well versed in IP addressing. In addition, you should also know a bit more about clients and servers and how connections are established using ports.
6.1.2 Clients and servers
Anyone who has ever used a web browser is familiar with the client/server computing model. Data, in one format or another, is stored on a centralized, powerful server. Cli- ents then connect to that server using a designated protocol (such as HTTP) to retrieve the data and work with it.
This pattern is of course much older than the web, and it has been applied for everything from completely dumb terminals connecting to mainframes to modern desktop applications that connect to a server for only a portion of their purpose (such as with iTunes, which is primarily a media organizer and player but also has a store where customers can connect to the server to get new content). In any case, the con- cept is the same: the client makes a type of request to the server and the server responds. This is the same model that the majority of Android applications, at least
those that use a server side at all, generally follow (Android applications typically end up as the client).
In order to handle many client requests, often for different purposes, coming in nearly simultaneously to a single IP address, modern server operating systems use the concept of ports. Ports are not physical; they are simply a representation of a particular area of the computer’s memory. A server can “listen” on multiple designated ports at a single address; for example, one port for sending email, one port for web traffic, two ports for file transfer, and so on. Every computer with an IP address also supports a range of thousands of ports to enable multiple “conversations” to happen at the same time.
Ports are divided into three ranges:
■ Well Known Ports —0 through 1023 ■ Registered Ports —1024 through 49151
■ Dynamic and/or Private Ports —49152 through 65535
The Well Known Ports are all published and are just that, well known. HTTP is port 80 (and HTTP Secure, or HTTPS, is port 443), FTP is ports 20 (control) and 21 (data), SSH is port 22, SMTP is port 25, and so on.
Beyond the Well Known Ports, the Registered Ports are still controlled and pub- lished but for more specific purposes. Often these ports are used for a particular application or company; for example, MySQL is port 3306 (by default). For a complete list of Well Known and Registered Ports, see the ICANN port-numbers document: http://www.iana.org/assignments/port-numbers.
The Dynamic or Private Ports are intentionally unregistered because they are used by the TCP/IP stack to facilitate communication. These ports are dynamically regis- tered on each computer and used in the conversation. Dynamic port 49500, for exam- ple, might be used to handle sending a request to a web server and dealing with the response. Once the conversation is over, the port is reclaimed and can be reused, locally, for any other data transfer.
Clients and servers therefore communicate as nodes with addresses, using ports, on a network that supports various protocols. The protocols involved with Android are based on the IP network the platform is designed to participate in and involve the TCP/IP family. Before we can build a full-on client/server Android application using the network, we need to handle the prerequisite task of determining the state of the connection.
6.2
Checking the network status
Android provides a host of utilities to determine the device configuration and the status of various services, including the network. You will typically use the
ConnectivityManager class to determine whether there is network connectivity and to get notifications of network changes. Listing 6.1, a portion of the main
Activity in the NetworkExplorer application, demonstrates basic usage of the
173