CFSocket

Overview

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.

Topics

Creating Sockets

CFSocketCreate

Creates a CFSocket object of a specified protocol and type.

CFSocketCreateConnectedToSocketSignature

Creates a CFSocket object and opens a connection to a remote socket.

CFSocketCreateWithNative

Creates a CFSocket object for a pre-existing native socket.

CFSocketCreateWithSocketSignature

Creates a CFSocket object using information from a CFSocketSignature structure.

Configuring Sockets

CFSocketCopyAddress

Returns the local address of a CFSocket object.

CFSocketCopyPeerAddress

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

CFSocketDisableCallBacks

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

CFSocketEnableCallBacks

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

CFSocketGetContext

Returns the context information for a CFSocket object.

CFSocketGetNative

Returns the native socket associated with a CFSocket object.

CFSocketGetSocketFlags

Returns flags that control certain behaviors of a CFSocket object.

CFSocketSetAddress

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

CFSocketSetSocketFlags

Sets flags that control certain behaviors of a CFSocket object.

Using Sockets

CFSocketConnectToAddress

Opens a connection to a remote socket.

CFSocketCreateRunLoopSource

Creates a CFRunLoopSource object for a CFSocket object.

CFSocketGetTypeID

Returns the type identifier for the CFSocket opaque type.

CFSocketInvalidate

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

CFSocketIsValid

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

CFSocketSendData

Sends data over a CFSocket object.

Callbacks

CFSocketCallBack

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

Data Types

CFSocketContext

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

CFSocketNativeHandle

Type for the platform-specific native socket handle.

CFSocketRef

A reference to a CFSocket object.

CFSocketSignature

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

Constants

CFSocketCallBackType

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.

CFSocketError

Error codes for many CFSocket functions.

See Also

Opaque Types

CFFileDescriptor

The CFFileDescriptor provides an opaque type to monitor file descriptors for read and write activity via CFRunLoop.

CFStringTokenizer

CFStringTokenizer allows you to tokenize strings into words, sentences or paragraphs in a language-neutral way. It supports languages such as Japanese and Chinese that do not delimit words by spaces, as well as de-compounding German compounds. You can obtain Latin transcription for tokens. It also provides language identification API.