www.it-ebooks.info iOS and OS X Network Programming Cookbook Over 50 recipes to develop network applications in both the iOS and OS X environment Jon Hoffman BIRMINGHAM - MUMBAI www.it-ebooks.info iOS and OS X Network Programming Cookbook Copyright © 2014 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: January 2014 Production Reference: 1150114 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-84969-808-5 www.packtpub.com Cover Image by Jarosław Blaminsky ([email protected]) www.it-ebooks.info Credits Author Project Coordinator Jon Hoffman Joel Goveya Reviewers Proofreader Chady Kassouf Joanna McMahon Shahin Katebi Indexer Josh Rufer Monica Ajmera Mehta Acquisition Editor Graphics Vinay Argekar Yuvraj Mannari Abhinash Sahu Lead Technical Editor Ritika Dewani Production Coordinator Adonia Jones Technical Editors Pratik More Cover Work Shweta Pant Adonia Jones Ritika Singh Nachiket Vartak Copy Editors Dipti Kapadia Kirti Pai Shambhavi Pai www.it-ebooks.info About the Author Jon Hoffman has close to 20 years of experience in the field of Information Technology. Over these 20 years, Jon has worked in the areas of system administration, network administration, network security, development and architecture. Currently, he works as a software engineer at Syn-Tech Systems. He has started a network development blog at http://network-development.blogspot.com that will enhance and expand on the material covered in this book. Over the past five years, he has developed numerous applications for the iOS platform. These include several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. What really drives Jon are the challenges in Information Technology; there is nothing more exhilarating for him than overcoming a challenge. Some of Jon's other interests are watching baseball (Go Sox!) and basketball (Go Celtics!). Jon also really enjoys Taekwondo; he and his eldest daughter Kailey are on pace to get their black belts together in the spring of 2014. I would like to thank my wonderful wife Kim, without whose support, encouragement, patience, and understanding, this book would have never been written. I would also like to thank my two wonderful daughters Kailey and Kara, who have both been my inspiration and driving force since they were born. To my dog, Buddy, maybe one day I will be the person who he thinks I am. I would like to give special thanks to all of the wonderful people at Packt Publishing who have helped me along the way. www.it-ebooks.info About the Reviewers Chady Kassouf is an independent iOS and web development expert. He started programming 21 years ago and hasn't stopped since. Five years ago, he decided to leave his job as a team leader in one of the leading digital agencies, and started his own business. His interests outside of computers include arts, music, and fitness. He can be found online at http://chady.net/. Shahin Katebi is a software architect and developer with 10 years of experience in creating apps for various platforms (Mac, iOS, Windows, and the Web). He works as a mobile solutions consultant with different companies, and also works with some startup teams worldwide. He teaches iOS/Mac OS development, and as a mentor at Startup Weekend events, helps startup teams make their own business. He is the founder and team leader at Seeb Co. (http://seeb.co/), a creative mobile app development organization creating apps for customers around the world. www.it-ebooks.info Josh Rufer attended university and majored in graphic arts. As passionate as he was for his traditional artwork, he found far more enjoyment in the art of human interaction. His first position was as the junior interface designer for a small XP programming group. Without enough work to keep him busy, he quickly outpaced his job title and was promoted to senior user experience engineer. On enhancing his programming skills in Java and C++, he was promoted to the position of junior programmer and again promoted as a senior software engineer. Always looking for more challenges, he formed a one-man design and software engineering firm called Guy Writes Code. This allowed him to focus his free time on the things that most interested him: designing and development for the iPhone and iPad. He has created several public applications for companies such as Metabahn and Camdilleo Media. He is currently working on iPad-based training and simulation applications, including augmented reality training on the iPad. When possible, Josh has helped with fact checking and technical editing for books such as Deploying with JRuby by Joe Kutner. www.it-ebooks.info www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? f Fully searchable across every book published by Packt f Copy and paste, print and bookmark content f On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info www.it-ebooks.info Table of Contents Preface 1 Chapter 1: BSD Socket Library 7 Introduction 7 Finding the byte order of your device 10 Retrieving network address information 12 Performing a network address resolution 16 Creating an echo server 22 Creating an echo client 31 Creating a data server 38 Creating a data client 42 Chapter 2: Apple Low-level Networking 45 Introduction 45 Retrieving network address information 46 Performing a network address resolution 48 Creating an echo server 55 Creating an echo client 63 Creating a server to receive data 68 Creating a client to send data 76 Checking the network status 78 Chapter 3: Using Libnet 83 Introduction 83 Installing libnet 89 Adding libnet to your project 91 Resolving names to addresses with libnet 93 Retrieving local addresses with libnet 97 Constructing a Ping packet with libnet 100 www.it-ebooks.info
Description: