iOS and OS X Network Programming Cookbook

Book description

If you want to develop network applications for iOS and OS X, this is one of the few books written specifically for those systems. With over 50 recipes and in-depth explanations, it's an essential guide.

In Detail

Darwin, which forms the core set of components for OS X and iOS, is compatible with the Single UNIX Specification Version 3 and POSIX UNIX. Therefore OS X and iOS are UNIX operating systems. While there are numerous books and articles written to teach network development in a UNIX environment there is surprisingly little dedicated to teaching network development specifically in an Apple environment. In this book we will be covering the standard UNIX networking stack with BSD Sockets as well as Apple's frameworks like CFNetworking and Bonjour.

"iOS and OS X Network Programming Cookbook" is an enthralling code-focused book on developing client and server applications for the iOS and OS X environments. You will learn how to integrate a number of Unix (BSD Sockets), Apple specific (CFNetworking, NSMutableURLRequest, Bonjour) and third party APIs (AFNetworking, MKNetworking) with your own apps.

"iOS and OS X Network Programming Cookbook" begins by showing you how to build both client and server applications using the BSD Socket Library as well as Apple's CFNetworking library. The sample code includes samples for both iOS and OS X.

It then covers how to add Bonjour to publish, find, and connect to services. This can be used to create peer-to-peer applications over local networks. Finally it shows how to integrate third-party libraries like Libnet, LibPCAP, AFNetworking, and MKNetworking with the reader's apps.

A lot of developers do not realize that iOS is a full UNIX-based operating system that contains a full set of networking APIs. These APIs can be used to create both client- and server-based applications in the iOS environment as well as OS X. Using the Apple's Bonjour library you will learn how to publish, find, and connect the services together without hardcoding the connection information. There are also a number of third-party APIs that can be used to easily connect iOS and OS X applications to web-based services. Finally this book will cover several low level libraries like LibNET and LibPCAP on the OS X environment.

"iOS and OS X Network Programming Cookbook" will cover several UNIX, Apple, and third-party libraries and APIs. The book will show the user how to integrate all of these libraries and APIs with their applications.

What You Will Learn

  • Create client- and server-based applications with the BSD socket API
  • Create client- and server-based applications with CFNetworking
  • Publish, find, and connect client applications with servers using Apple's Bonjour library
  • Use the AFNetworking library to connect to web-based services
  • Connect to web-based services using the MKNetworking library
  • Explore the LibNET packet construction library in OS X
  • Get to grips with the LibPCAP packet capture library in OS X
  • Discover Apple's high-level APIs to connect to devices via Bluetooth
  • Connect two devices with a Bluetooth connection

Table of contents

  1. iOS and OS X Network Programming Cookbook
    1. Table of Contents
    2. iOS and OS X Network Programming Cookbook
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. BSD Socket Library
      1. Introduction
      2. Finding the byte order of your device
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Retrieving network address information
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Performing a network address resolution
        1. Getting ready
        2. How to do it…
          1. Creating the AddrInfo header file
          2. Creating the AddrInfo implementation file
          3. Using the AddrInfo class to perform the address/hostname resolution
        3. How it works…
      5. Creating an echo server
        1. Getting ready
        2. How to do it….
          1. Creating the BSDSocketServer header file
          2. Creating the BSDSocketServer implementation file
          3. Using the BSDSocketServer class to start the echo server
        3. How it works…
      6. Creating an echo client
        1. Getting ready
        2. How to do it…
          1. Creating the BSDSocketClient header file
          2. Creating the BSDSocketClient implementation file
          3. Using the BSDSocketClient to connect to our echo server
        3. How it works…
      7. Creating a data server
        1. Getting ready
        2. How to do it…
          1. Updating the BSDSocketServer header file
          2. Updating the BSDSocketServer implementation file
          3. Using the BSDSocketServer to create our data server
        3. How it works…
      8. Creating a data client
        1. Getting ready
        2. How to do it…
          1. Updating the BSDSocketClient header file
          2. Updating the BSDSocketClient implementation file
          3. Using the BSDSocketClient to connect to our data server
        3. How it works…
    9. 2. Apple Low-level Networking
      1. Introduction
      2. Retrieving network address information
        1. Getting ready
        2. How to do it...
        3. How it works…
      3. Performing a network address resolution
        1. Getting ready
        2. How to do it…
          1. Creating the CFNetworkUtilities header file
          2. Creating the CFNetworkUtilities implementation file
        3. How it works...
      4. Creating an echo server
        1. Getting ready
        2. How to do it...
          1. Creating the CFSocketServer header file
          2. Creating the CFSocketServer implementation file
        3. How it works...
      5. Creating an echo client
        1. Getting ready
        2. How to do it...
          1. Creating the CFSocketClient header file
          2. Creating the CFSocketClient implementation file
          3. Using the CFSocketClient class
        3. How it works...
      6. Creating a server to receive data
        1. Getting ready
        2. How to do it…
          1. Updating the CFSocketServer header file
          2. Updating the CFSocketServer implementation file
          3. Using the CFSocketServer class
        3. How it works...
      7. Creating a client to send data
        1. Getting ready
        2. How to do it...
          1. Updating the CFSocketClient header file
          2. Updating the CFSocketClient implementation file
        3. How it works...
      8. Checking the network status
        1. Getting ready
        2. How to do it...
          1. Creating the NetworkDetect header file
          2. Creating the NetworkDetect implementation file
        3. How it works...
    10. 3. Using Libnet
      1. Introduction
      2. Installing libnet
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Adding libnet to your project
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Resolving names to addresses with libnet
        1. Getting ready
        2. How to do it…
          1. Importing the libnet header
        3. How it works…
      5. Retrieving local addresses with libnet
        1. Getting ready
        2. How to do it…
          1. Importing the libnet header
          2. Retrieving the local IP and hardware addresses of our device
        3. How it works…
      6. Constructing a Ping packet with libnet
        1. Getting ready
        2. How to do it…
          1. Importing the libnet header
          2. Defining variables
          3. Initiating the libnet context
          4. Setting the target and source IP addresses
          5. Creating a random number to be used as an identifier
          6. Building the ICMP header
          7. Building the IPv4 header
          8. Injecting the ICMP packet
        3. How it works…
      7. Constructing a UDP packet with libnet
        1. Getting ready
        2. How to do it…
          1. Importing the libnet header
          2. Initiating the libnet context
          3. Setting the target and source IP addresses
          4. Creating a random number to be used as an identifier
          5. Building the UDP header
          6. Building the IPv4 header
          7. Injecting the packet
        3. How it works…
      8. Constructing a TCP packet with libnet
        1. Getting ready
        2. How to do it…
          1. Importing the libnet header
          2. Defining variables
          3. Initiating the libnet context
          4. Setting the target and source IP addresses
          5. Creating a random number to be used as an identifier
          6. Building the TCP header
          7. Building the IPv4 header
          8. Injecting the packet
        3. How it works…
    11. 4. Using Libpcap
      1. Introduction
      2. Adding libpcap to your project
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Retrieving network device information
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Capturing packets
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Decoding Ethernet headers
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Decoding IP headers
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Decoding ARP headers
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. Decoding TCP headers
        1. Getting ready
        2. How to do it…
        3. How it works…
      9. Decoding UDP headers
        1. Getting ready
        2. How to do it…
        3. How it works…
      10. Decoding ICMP headers
        1. Getting ready
        2. How to do it…
        3. How it works…
      11. Filtering packets
        1. Getting ready
        2. How to do it…
        3. How it works…
      12. Saving a capture file
        1. Getting ready
        2. How to do it…
        3. How it works…
      13. Creating a simple port scanner using libnet and libpcap together
        1. Getting ready
        2. How to do it…
        3. How it works…
    12. 5. Apple High-level Networking
      1. Introduction
      2. Performing HTTP(S) synchronous GET requests
        1. Getting ready
        2. How to do it…
          1. Creating the WebServiceConnectSynchronous header file
          2. Creating the sendGetRequest:toURL: method
        3. How it works…
      3. Performing HTTP(S) synchronous POST requests
        1. Getting ready
        2. How to do it…
          1. Updating the WebServiceConnectSynchronous header file
          2. Creating the sendPostRequest:toUrl: method
        3. How it works…
      4. Performing HTTP(S) asynchronous GET requests
        1. Getting ready
        2. How to do it…
          1. Creating the WebServiceConnectAsynchronous header file
          2. Creating the sendGetRequest:toURL: method
          3. Creating the connection:didReceiveResponse: callback method
          4. Creating the connection:didReceiveData: callback method
          5. Creating the connection:didFailWithError: callback method
          6. Creating the connectionDidFinishLoading: callback method
          7. Creating the connection:willSendRedirect:redirectResponse: callback method
          8. Creating the connection:willCacheResponse: callback method
        3. How it works…
      5. Performing HTTP(S) asynchronous POST requests
        1. Getting ready
        2. How to do it…
          1. Updating the WebServiceConnectAsynchronous header file
          2. Creating the sendPostRequest:toURL: method
        3. How it works…
      6. Parsing an RSS feed with NSXMLParser, NSURL, and NSData
        1. Getting ready
        2. How to do it…
          1. Creating the RSSItem header file
          2. Creating the RSSItem implementation file
          3. Creating the ParseRSS header file
          4. Creating the initWithUrl: constructor
          5. Creating the parseXMLFileAtUrl: method
          6. Creating the parserDidStartDocument: NSXMLParserDelegate method
          7. Creating the parser:parserErrorOccurred: NSXMLParserDelegate method
          8. Creating the parser:didStartElement:namespaceURI:qualifiedName:attributes: NSXMLParserDelegate method
          9. Creating the parser:didEndElement:namespaceURI:qualifiedName: NSXMLParserDelegate method
          10. Creating the parser:foundCharacters: NSXMLParserDelegate method
          11. Creating the parserDidEndDocument: NSXMLParserDelegate method
        3. How it works…
      7. Creating a peer-to-peer bluetooth network
        1. Getting ready
        2. How to do it…
          1. Displaying the GKPeerPickerController
          2. Creating the two delegate methods for GKPeerPickerControllerDelegate
          3. Disconnecting from the peer network
          4. Sending data to the peer
          5. Receiving data from the peer
        3. How it works…
    13. 6. Bonjour
      1. Introduction
      2. Publishing a Bonjour service
        1. Getting ready
        2. How to do it...
        3. How it works...
      3. Discovering a Bonjour service
        1. Getting ready
        2. How to do it...
        3. How it works...
      4. Resolving a Bonjour service
        1. Getting ready
        2. How to do it...
        3. How it works...
      5. Creating an echo server that uses Bonjour to advertise the service
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. Creating an echo client that uses Bonjour to discover the service
        1. Getting ready
        2. How to do it...
        3. How it works...
    14. 7. AFNetworking 2.0 Library
      1. Introduction
      2. Checking the network connection type and changes
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Creating a web client using AFHTTPSessionManager
        1. Getting ready
        2. How to do it…
          1. Creating the ITunesClient header file
          2. Creating the sharedClient method
          3. Creating the searchType:withTerm:completion: method
          4. Using the ITunesClient class
        3. How it works…
      4. Creating a custom response serializer
        1. Getting ready
        2. How to do it…
          1. Creating the AlbumInformation header file
          2. Creating the AlbumInformation implementation file
          3. Creating the ITunesResponseSerializer header file
          4. Creating the ITunesResponseSerializer implementation file
          5. Adding ITunesResponseSerializer to our ITunesClient
          6. Using the new ITunesClient class
        3. How it works…
      5. Using the UIImageView+AFNetworking category
        1. Getting ready
        2. How to do it…
          1. Updating the ViewController header file
          2. Updating the viewDidLoad method of the ViewController
          3. Creating the UITableView delegate methods
        3. How it works…
      6. Downloading files with a progress bar
        1. Getting ready
        2. How to do it…
          1. Creating the download task
          2. Adding the progressView
        3. How it works…
    15. 8. MKNetworkKit
      1. Introduction
      2. Creating and using the MKNetworkKit engine
        1. Getting ready
        2. How to do it…
          1. Creating the ITunesEngine header file
          2. Creating the ITunesEngine implementation file
          3. Using the ITunesEngine class
        3. How it works…
      3. Uploading a file using MKNetworkKit
        1. Getting ready
        2. How to do it…
          1. Creating the FileUploadEngine header file
          2. Creating the FileUploadEngine implementation file
          3. Using the FileUploadEngine class
        3. How it works…
      4. Downloading a file using MKNetworkKit
        1. Getting ready
        2. How to do it…
          1. Creating the ImageDownloadEngine header file
          2. Creating the ImageDownloadEngine implementation file
        3. How it works…
      5. Using the UIImageView+MKNetworkKitAdditions.h category and caching the images
        1. Getting ready
        2. How to do it…
          1. Creating the ITunesEngine header file
          2. Creating the ITunesEngine implementation file
          3. Adjusting the cache settings
          4. Using the new ITunesEngine class
        3. How it works…
      6. Adding a progress bar to upload or download
        1. Getting ready
        2. How to do it…
          1. Creating the FileDownloadEngine header file
          2. Creating the FileDownloadEngine implementation file
        3. How it works…
    16. Index

Product information

  • Title: iOS and OS X Network Programming Cookbook
  • Author(s): Jon Hoffman
  • Release date: January 2014
  • Publisher(s): Packt Publishing
  • ISBN: 9781849698085