- iOS and OS X Network Programming Cookbook
- Jon Hoffman
- 369字
- 2021-07-19 18:40:40
Introduction
The primary API behind Apple's low-level networking is the CFNetwork API.
The simplest way to describe CFNetworking is to say that it is an Apple-specific extension to the BSD socket API. The CFNetworking stack is based on and relies on the BSD socket API that was discussed in Chapter 1, BSD Socket Library. It is recommended that the reader understands the concepts discussed in Chapter 1, BSD Socket Library, prior to going through this chapter. While this chapter will focus primarily on CFNetworking for most of the recipes, we will also use NSHost
and the system configuration framework for retrieving network address information and checking the network status recipes. The biggest advantage that BSD sockets have over CFNetwork is the compatibility with other forms of Unix. This is a pretty big advantage when you think of all the BSD socket code on the Internet that you can use. However, if your application is Apple-specific, it is recommended that you use CFNetwork wherever you can.
CFNetwork offers numerous advantages over BSD sockets. The biggest advantage is the run-loop integration. So if your application is run-loop-based, you will be able to implement network services without implementing numerous threads.
CFNetwork also contains a number of objects to help you implement specific protocols without having to know the implementation details about the protocols. This includes the CFFTP API to assist in implementing the FTP protocol, and CFHTTP to assist in implementing the HTTP protocol.
To understand CFNetworking, you should be aware of the main building blocks that make up CFNetwork, which are as follows:
- CFSocket: It is an abstraction of the BSD socket covered in Chapter 1, BSD Socket Library. One of the main differences between the BSD socket and the CFSocket is that the CFSocket can be integrated with a run loop.
- CFStream: It provides both read and write streams and makes it easy to exchange data not only across networks but also with files and memory objects.
- CFSocketStream: It provides an extension for CFStream to work with network sockets.
- CFFTP: It provides an API for communicating with FTP servers.
- CFHTTP: It provides an API for communicating with HTTP servers.
- CFHTTPAuthentication: It provides an API for responding to HTTP authentication challenges.