< Previous PageNext Page > Hide TOC

Networking in Mac OS X

In most cases, networking means communicating with remote locations over the Internet, and that's what this document is largely about. If that's not what you're interested in, check "Related Subjects" toward the end of this document.

The rest of this document will talk about the various Mac OS X APIs that enable you to connect to and communicate with remote locations on the Internet:

Before reading about these APIs, you first need to understand a concept called run loops.

Contents:

Threads vs. Run Loops
Mac OS X Networking APIs
Related Subjects
For Further Information


Threads vs. Run Loops

Many socket-based programs are designed around the use of threads. For these programs, Apple provides similar functionality to facilitate porting the code to Mac OS X. Code using BSD sockets should require only minor changes to compile and run under Mac OS X.

One of the complexities of socket use involves the use of separate threads to handle the exchange of data. In a server environment, where hundreds of sockets are open simultaneously, the code that checks all these threads for activity may cause a significant performance hit. In addition, with threads, data synchronization is often an issue, and locks are often required to guarantee that only one thread has access to a given global variable at a time.

For such situations, Apple provides run loops, which behave like the message loop in a Win32 application. You can use run loops to handle various input sources, including sockets. Each thread of execution has exactly one run loop, which can handle multiple input sources. When you add an input source (for example, a socket) to a thread's run loop, you must also hand it a callback function that is to be called whenever the input source needs attention.

Event-driven code is more complex than code using blocking threads, but it delivers the highest network performance. When you use a run loop to handle multiple sockets in the same thread, you avoid the data synchronization problems associated with a multiple-thread solution. The CFSocket API contains routines that hand off the servicing of a socket to the thread's run loop.

Mac OS X Networking APIs

The following sections describe the three networking APIs you have available to you. The two higher-level APIs, CFNetwork and CFSocket, are both built on top of the third API, BSD Sockets. Each API is most useful in certain situations, so you need to decide which one to use. Documentation for these APIs resides in several places; see "For Further Information" at the end of this document for details.

BSD Sockets

BSD Sockets is the traditional UNIX API for connecting to other system processes and resources that reside on the Internet. If your Win32 code uses the Winsock API (which is itself based on BSD Sockets) for synchronous communication, you should have little trouble porting it to the Mac OS X implementation of BSD Sockets. The BSD Sockets API includes such procedures as socket, bind, listen, accept, connect, read, write, and close.

CFSocket

CFSocket provides a high-level API that uses BSD sockets to communicate with a remote Internet location, while also providing a configurable mechanism (using callback routines and the current thread's run loop) for automatically handling the data that the remote Internet location returns. For example you can:

You will probably want to use the CFSocket API if your current networking code uses Winsock in its asynchronous mode.

CFNetwork

CFNetwork is a high-level networking API that makes it easy to create, send, and receive the kinds of messages that are commonly exchanged between clients and servers. It currently includes support for HTTP messages; support for other protocols (including SMTP, LDAP, and FTP) is planned for the future.

CFNetwork has functions for

The following list gives you an idea of how to work with CFNetwork by showing you the routines you would use to create and send an HTTP request message.

Related Subjects

Networking means different things to different people. The subsections below introduce two network-related resources you may find of interest. Pointers to relevant documentation are given in "For Further Information," below.

System Configuration Framework

This framework is used system-wide to allow various system resources to be configured under program control. In the context of networking, you might use this framework to configure network settings for the user and to change them automatically as network services are added and removed.

Network Services

Apple's Rendezvous technology, first made available in Mac OS X version 10.2 (Jaguar), enables both wireless and wire-connected computers, peripherals, and consumer devices to automatically connect to an ad hoc network. If you are interested in building this technology into your application, CFNetwork includes a Network Services API, CFNetServices, for doing so.

Network Services (also called CFNetServices) makes it possible for you to register your network service, along with a human-readable name and all the information needed to connect to your service. You can also create a network service browser, which enables you to browse for other registered services that you can connect to.

I/O Kit

If your interest in networking relates to writing software to communicate with associated networking hardware (for example, device drivers for a network interface card), you need to know about the I/O Kit. This is a collection of frameworks, libraries, tools, and other resources for creating device drivers for Mac OS X. The I/O Kit uses an object-oriented approach and is designed to make the creation of device drivers faster and easier.

For Further Information

Most of the documentation for Mac OS X's networking APIs is contained in header files, man pages, and books on UNIX network programming. See the notes below for help on accessing these forms of documentation.

CFRunLoop documentation in header file

CFRunLoop.h

BSD Sockets

See the man pages for individual commands.

One recommended book on the subject is UNIX Network Programming, Network APIs: Sockets and XTI, Volume 1, 2nd Edition, by W. Richard Stevens, published by Prentice Hall, ISBN 0-13-490012-X.

Microsoft Winsock 2.2 API documentation (includes "Deviation from BSD Sockets" section)

ftp://ftp.microsoft.com/bussys/winsock/winsock2/WSAPI22.DOC

CFSocket documentation in header file

CFSocket.h

CFNetwork documentation

CFNetwork Programming Guide

CFNetwork documentation in header files

CFNetwork.h, CFHTTPMessage.h, CFHTTPStream.h, CFSocketStream.h, and CFNetServices.h

Release Notes for CFNetwork and CFNetServices in Mac OS X v10.2

CFNetwork Framework Release Notes

Hardware & Drivers Documentation

Guides > Hardware & Drivers

I/O Kit Fundamentals book

I/O Kit Fundamentals

Networking sessions at WWDC 2002

* session 808--Managing I/O: CFRunLoop and CFStream * session 803--Mac OS X Networking Overview * session 805--Introducing CFNetwork

available for purchase at http://developer.apple.com/adctv/

Note: As on any UNIX system, you can access the BSD Sockets commands by opening a Terminal window and typing "man <commandname>". Header files are located in various places, so the best way to find one is to type "locate <filename>" in a Terminal window. If the command doesn't work, you need to build the underlying search database; see http://osxfaq.com/Tutorials/LearningCenter/UnixTutorials/WorkingWithUnix/page2.ws for instructions on how to do this.



< Previous PageNext Page > Hide TOC


© 2003, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-05-06)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.