A CFSocket is a communications channel implemented with a BSD socket.

For most uses of this API, you will need to include three headers:

#import <CoreFoundation/CoreFoundation.h> #include <sys/socket.h> #include <netinet/in.h>

CFSocket can be created from scratch with CFSocketCreate(_:_:_:_:_:_:_:) and CFSocketCreateWithSocketSignature(_:_:_:_:_:). CFSocket objects can also be created to wrap an existing BSD socket by calling CFSocketCreateWithNative(_:_:_:_:_:). Finally, you can create a CFSocket and connect simultaneously to a remote host by calling CFSocketCreateConnectedToSocketSignature(_:_:_:_:_:_:).

To listen for messages, you need to create a run loop source with CFSocketCreateRunLoopSource(_:_:_:) and add it to a run loop with CFRunLoopAddSource(_:_:_:). You can select the types of socket activities, such as connection attempts or data arrivals, that cause the source to fire and invoke your CFSocket’s callback function. To send data, you store the data in a CFData and call CFSocketSendData(_:_:_:_:).

Unlike Mach and message ports, sockets support communication over a network.


Configuring Sockets

func CFSocketCopyAddress(CFSocket!)

Returns the local address of a CFSocket object.

func CFSocketCopyPeerAddress(CFSocket!)

Returns the remote address to which a CFSocket object is connected.

func CFSocketDisableCallBacks(CFSocket!, CFOptionFlags)

Disables the callback function of a CFSocket object for certain types of socket activity.

func CFSocketEnableCallBacks(CFSocket!, CFOptionFlags)

Enables the callback function of a CFSocket object for certain types of socket activity.

func CFSocketGetContext(CFSocket!, UnsafeMutablePointer<CFSocketContext>!)

Returns the context information for a CFSocket object.

func CFSocketGetNative(CFSocket!)

Returns the native socket associated with a CFSocket object.

func CFSocketGetSocketFlags(CFSocket!)

Returns flags that control certain behaviors of a CFSocket object.

func CFSocketSetAddress(CFSocket!, CFData!)

Binds a local address to a CFSocket object and configures it for listening.

func CFSocketSetSocketFlags(CFSocket!, CFOptionFlags)

Sets flags that control certain behaviors of a CFSocket object.

Using Sockets

func CFSocketCreateRunLoopSource(CFAllocator!, CFSocket!, CFIndex)

Creates a CFRunLoopSource object for a CFSocket object.

func CFSocketGetTypeID()

Returns the type identifier for the CFSocket opaque type.

func CFSocketInvalidate(CFSocket!)

Invalidates a CFSocket object, stopping it from sending or receiving any more messages.

func CFSocketIsValid(CFSocket!)

Returns a Boolean value that indicates whether a CFSocket object is valid and able to send or receive messages.



Callback invoked when certain types of activity takes place on a CFSocket object.

Data Types


A structure that contains program-defined data and callbacks with which you can configure a CFSocket object’s behavior.


Type for the platform-specific native socket handle.


A reference to a CFSocket object.


A structure that fully specifies the communication protocol and connection address of a CFSocket object.



Types of socket activity that can cause the callback function of a CFSocket object to be called.

CFSocket Flags

Flags that can be set on a CFSocket object to control its behavior.


Error codes for many CFSocket functions.