Book Image

iOS and OS X Network Programming Cookbook

By : Jon Hoffman
Book Image

iOS and OS X Network Programming Cookbook

By: Jon Hoffman

Overview of this book

Table of Contents (15 chapters)
iOS and OS X Network Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Darwin forms the core set of components for OS X and iOS, and is compatible with Single UNIX Specification Version 3 and POSIX UNIX. Therefore, OS X and iOS are considered to be Unix operating systems. This means that OS X and iOS use the same basic networking stack that all Unix operating systems use.

Apple has added several frameworks on top of the basic Unix networking stack. This includes frameworks such as CFNetworking and Bonjour, as well as classes such as NSURLConnection. There are also several outstanding third-party frameworks written specifically for OS X and/or iOS.

There are numerous books written to teach network development in a Unix environment. However, it is hard to find books dedicated to teaching network development, specifically in an Apple environment that discusses Apple-specific libraries and frameworks. Using and understanding these frameworks can greatly reduce the time needed to add network components to our applications.

This book will begin by discussing the lower-level frameworks, such as BSD Sockets and CFNetworking. Higher-level frameworks and third-party libraries are based on these frameworks, so understanding how they work is essential for understanding how the higher-level libraries work.

We will then look at two libraries, one to construct and inject network packets, and another to capture incoming packets. These libraries are specific to OS X development. We will then look at Apple's higher-level frameworks followed by two outstanding third-party frameworks.

What this book covers

Chapter 1, BSD Socket Library, shows the reader how they can use the BSD Socket Library in their iOS and OS X applications. While this chapter will show them how to obtain network address information and also how to check the network status, the primary focus will be on creating client/server applications for both iOS and OS X devices. We will be creating server applications for iOS devices. This is a very important chapter for the reader because every other API is directly or indirectly based on the BSD Socket Library.

Chapter 2, Apple Low-level Networking, will show the reader how to obtain network address information but the primary focus will be on creating client/server applications for iOS and OS X devices. CFNetworking is Apple's wrapper around the BSD Socket Library. These APIs are designed for easier usage, to integrate better with run loops, and they contain a number of classes to help implement various protocols without having to know the details of those protocols.

Chapter 3, Using Libnet, shows the reader how to use libnet to retrieve network address information, perform network address resolution, and also to manually construct network packets. The chapter is written specifically for OS X. Libnet is a packet construction library that allows the developer to manually create and send out individual packets.

Chapter 4, Using Libpcap, shows how to use libpcap with an OS X application and will end by building a utility to capture packets. This chapter is written specifically for OS X. Libpcap is a packet-capture library that has been complied for virtually every Unix/Linux distribution, and this includes the OS X environment, but unfortunately it does not include iOS.

Chapter 5, Apple High-level Networking, covers some of Apple's higher-level APIs that can be used for specific purposes. This includes Synchronous and Asynchronous HTTP connections for retrieving XML feeds and also the Bluetooth connectivity between two devices.

Chapter 6, Bonjour, shows the reader how they can implement Bonjour network services in their applications. By the end of the chapter, the reader will be able to implement Bonjour services in their application.

Chapter 7, AFNetworking 2.0 Library, shows the reader how to retrieve and send text as well as data to and from remote servers by using the AFNetworking library. AFNetworking is an amazing network library for iOS and OS X. It is built on top of Apple's foundation framework and is incredibly easy to use.

Chapter 8, MKNetworkKit, shows the reader how to retrieve and send text as well as data to and from remote servers by using the MKNetworkKit library. MKNetworkKit is an awesome networking framework written in Objective-C. The framework is based on blocks and is ARC ready.

What you need for this book

To follow the examples in this book, the reader should have a good understanding of iOS and OS X development techniques, as well as a good understanding of Objective-C and the Xcode development environment. It is also recommended that the reader have at least a basic understanding of TCP networks and how they work.

Readers should have an Apple computer with OS X 10.8 or higher installed. They also need to install Xcode Version 4.3.2 or higher.

Who this book is for

This book is written for both Enterprise and App Store developers who are interested in adding networking components to their applications. The examples in this book, with the exception of Chapter 2, Apple Low-level Networking, and Chapter 3, Using Libnet, can be applied to both OS X and iOS developers.

Enterprise developers will find the examples in the book extremely helpful while connecting their applications with the backend servers. Whether these connections are custom socket connections or web APIs, the examples in this book will be invaluable resources to an Enterprise developer.

iOS and OS X App Store developers will find the examples extremely helpful while adding network components to their applications. The examples in this book cover both peer-to-peer and client/server applications.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "This recipe will introduce libnet_init() and libnet_destroy() functions."

A block of code is set as follows:

libnet_t *lnet;
 u_int32_t target, source;
 u_int16_t id,seq;
 char payload[] = "Hello from libnet";
 char errbuf[LIBNET_ERRBUF_SIZE];

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

#import <Foundation/Foundation.h>
 
 #define LISTENQ 1024
 #define MAXLINE 4096
 
 typedef NS_ENUM(NSUInteger, BSDServerErrorCode) {
    NOERROR,
    SOCKETERROR,
    BINDERROR,
    LISTENERROR,
    ACCEPTINGERROR
}; 
 @interface BSDSocketServer : NSObject
 
 @property int errorCode, listenfd;
 
 -(id)initOnPort:(int)port;
 -(void)echoServerListenWithDescriptor:(int)lfd;
 -(void)dataServerListenWithDescriptor:(int)lfd;
 
 @end

Any command-line input or output is written as follows:

cd ~/Downloads
tar xopf libnet-1.2-rc2.tar
cd libnet-1.2-rc2

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "To run your project as root, from the top menu navigate to Project | Scheme | Edit Scheme as shown in the following screenshot:"

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.