Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
iOS and OS X Network Programming Cookbook

You're reading from   iOS and OS X Network Programming Cookbook 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.

Arrow left icon
Product type Paperback
Published in Jan 2014
Publisher
ISBN-13 9781849698085
Length 300 pages
Edition Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jon Hoffman Jon Hoffman
Author Profile Icon Jon Hoffman
Jon Hoffman
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

iOS and OS X Network Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. BSD Socket Library FREE CHAPTER 2. Apple Low-level Networking 3. Using Libnet 4. Using Libpcap 5. Apple High-level Networking 6. Bonjour 7. AFNetworking 2.0 Library 8. MKNetworkKit Index

Finding the byte order of your device


In the Introduction section of this chapter, one of the concepts that was briefly discussed was how devices store information in memory (byte order). After that discussion, you may be wondering what the byte order of your device is.

Tip

The byte order of a device depends on the Microprocessor architecture being used by the device. You can pretty easily go on to the Internet and search for "Mac OS X i386 byte order" and find out what the byte order is, but where is the fun in that? We are developers, so let's see if we can figure it out with code.

We can determine the byte order of our devices with a few lines of C code; however, like most of the code in this book, we will put the C code within an Objective-C wrapper to make it easy to port to your projects. The downloadable code for this chapter contains the Objective-C classes within an application to test your system.

Getting ready

This recipe is compatible with both iOS and OS X. No extra frameworks or libraries are required.

How to do it…

Let's get started by defining an ENUM in our header file:

  1. We create an ENUM that will be used to identify the byte order of the system as shown in the following code:

     typedef NS_ENUM(NSUInteger, EndianType) {
        ENDIAN_UNKNOWN,
        ENDIAN_LITTLE,
        ENDIAN_BIG
    };
  2. To determine the byte order of the device, we will use the byteOrder method as shown in the following code:

    -( EndianType)byteOrder {
        union {
            short sNum;
            char cNum[sizeof(short)];
        } un;
        un.sNum = 0x0102;
        if (sizeof(short) == 2) {
            if(un.cNum[0] == 1 && un.cNum[1] == 2)
                return ENDIAN_BIG;
            else if (un.cNum[0] == 2 && un.cNum[1] == 1)
                return ENDIAN_LITTLE;
            else
                return ENDIAN_UNKNOWN;
        } else
            return ENDIAN_UNKNOWN;
    }

Tip

Downloading the example code

You can download the example code files for all Packt Publishing 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.

How it works…

In the ByteOrder header file, we defined an ENUM with three constants. The constants are as follows:

  • ENDIAN_UNKNOWN: We are unable to determine the byte order of the device

  • ENDIAN_LITTLE: This specifies that the most significant bytes are last (little-endian)

  • ENDIAN_BIG: This specifies that the most significant bytes are first (big-endian)

The byteOrder method determines the byte order of our device and returns an integer that can be translated using the constants defined in the header file. To determine the byte order of our device, we begin by creating a union of short int and char[]. We then store the value 0x0102 in the union. Finally, we look at the character array to determine the order in which the integer was stored in the character array. If the number one was stored first, it means that the device uses big-endian; if the number two was stored first, it means that the device uses little-endian.

The downloadable code contains projects for both the Mac OS X and iOS devices, so you can see how to use this class and also test the byte order of your devices.

You have been reading a chapter from
iOS and OS X Network Programming Cookbook
Published in: Jan 2014
Publisher:
ISBN-13: 9781849698085
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image