Book Image

iOS and OS X Network Programming Cookbook

By : Jon Hoffman
Book Image

iOS and OS X Network Programming Cookbook

By: Jon Hoffman

Overview of this book

Table of Contents (15 chapters)
iOS and OS X Network Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating an echo server


In this recipe, we will be creating an echo server that will listen on a specified port. Once a connection is established, the server will echo any text received back by the client.

There are several ways to create a CFSocket. For this recipe, we will create a BSD socket and then use the CFSocketCreateWithNative() method to create the CFSocket from the native BSD socket.

To create a BSD socket, you must first create a socket using the socket() function. This function returns an integer descriptor that can be used to identify the socket for all future function calls. Once we have the socket descriptor, we need to bind the network interfaces and port to the socket. We create a sockaddr structure with the IP address version, IP address, and the port number to bind the socket. We will then call the bind() function to bind the sockaddr structure and the socket together. Finally, we will need to listen on the socket for new connections. This can be done with the listen()...