O'Reilly logo

Cocoa in a Nutshell by James Duncan Davidson, Michael Beam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sockets

It is possible to interact with Darwin’s BSD sockets API in all of Mac OS X’s C-based application environments, including Cocoa. This API is declared primarily in the headers sys/socket.h and netinet/in.h, and is discussed at length in Unix Network Programming, by W. Richard Stevens (Prentice Hall, 1998). Core Foundation also provides an API to sockets with CFSocket. However, discussion of CFSocket is beyond the scope this book. Instead, the next section provides shows how to interact with sockets using the Foundation class NSSocketPort.

In earlier versions of Mac OS X (prior to Mac OS X 10.2), NSSocketPort was used exclusively as part of Cocoa’s distributed objects architecture. NSSocketPort created sockets-based distributed objects connections across a network. However, now NSSocketPort provides a convenient alternative to the C sockets API for raw messaging.

NSSocketPort makes it possible to create sockets configured either as local listening sockets (server sockets) or sockets connected to a remote host (client sockets). The simplest way to initialize a listening socket port object is using the method initWithTCPPort:. This method takes a port number as an argument and returns an NSSocketPort object representing a TCP/IP streaming socket. If 0 is passed as the port number, then the operating system selects a port to bind to the socket.

Initialize an NSSocketPort to connect to a remote socket with the method initWithRemoteTCPPort:host:. This method takes as arguments the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required