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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access