<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreFoundation",
  "identifier" : "/documentation/CoreFoundation/CFSocket",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Core Foundation"
    ],
    "preciseIdentifier" : "c:@T@CFSocketRef"
  },
  "title" : "CFSocket"
}
-->

# CFSocket

```
class 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:

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

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

To listen for messages, you need to create a run loop source with [`CFSocketCreateRunLoopSource(_:_:_:)`](/documentation/CoreFoundation/CFSocketCreateRunLoopSource(_:_:_:)) and add it to a run loop with [`CFRunLoopAddSource(_:_:_:)`](/documentation/CoreFoundation/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(_:_:_:_:)`](/documentation/CoreFoundation/CFSocketSendData(_:_:_:_:)).

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

## Topics

### Creating Sockets

[`CFSocketCreate(_:_:_:_:_:_:_:)`](/documentation/CoreFoundation/CFSocketCreate(_:_:_:_:_:_:_:))

Creates a CFSocket object of a specified protocol and type.

[`CFSocketCreateConnectedToSocketSignature(_:_:_:_:_:_:)`](/documentation/CoreFoundation/CFSocketCreateConnectedToSocketSignature(_:_:_:_:_:_:))

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

[`CFSocketCreateWithNative(_:_:_:_:_:)`](/documentation/CoreFoundation/CFSocketCreateWithNative(_:_:_:_:_:))

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

[`CFSocketCreateWithSocketSignature(_:_:_:_:_:)`](/documentation/CoreFoundation/CFSocketCreateWithSocketSignature(_:_:_:_:_:))

Creates a CFSocket object using information from a CFSocketSignature structure.

### Configuring Sockets

[`CFSocketCopyAddress(_:)`](/documentation/CoreFoundation/CFSocketCopyAddress(_:))

Returns the local address of a CFSocket object.

[`CFSocketCopyPeerAddress(_:)`](/documentation/CoreFoundation/CFSocketCopyPeerAddress(_:))

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

[`CFSocketDisableCallBacks(_:_:)`](/documentation/CoreFoundation/CFSocketDisableCallBacks(_:_:))

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

[`CFSocketEnableCallBacks(_:_:)`](/documentation/CoreFoundation/CFSocketEnableCallBacks(_:_:))

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

[`CFSocketGetContext(_:_:)`](/documentation/CoreFoundation/CFSocketGetContext(_:_:))

Returns the context information for a CFSocket object.

[`CFSocketGetNative(_:)`](/documentation/CoreFoundation/CFSocketGetNative(_:))

Returns the native socket associated with a CFSocket object.

[`CFSocketGetSocketFlags(_:)`](/documentation/CoreFoundation/CFSocketGetSocketFlags(_:))

Returns flags that control certain behaviors of a CFSocket object.

[`CFSocketSetAddress(_:_:)`](/documentation/CoreFoundation/CFSocketSetAddress(_:_:))

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

[`CFSocketSetSocketFlags(_:_:)`](/documentation/CoreFoundation/CFSocketSetSocketFlags(_:_:))

Sets flags that control certain behaviors of a CFSocket object.

### Using Sockets

[`CFSocketConnectToAddress(_:_:_:)`](/documentation/CoreFoundation/CFSocketConnectToAddress(_:_:_:))

Opens a connection to a remote socket.

[`CFSocketCreateRunLoopSource(_:_:_:)`](/documentation/CoreFoundation/CFSocketCreateRunLoopSource(_:_:_:))

Creates a CFRunLoopSource object for a CFSocket object.

[`CFSocketGetTypeID()`](/documentation/CoreFoundation/CFSocketGetTypeID())

Returns the type identifier for the CFSocket opaque type.

[`CFSocketInvalidate(_:)`](/documentation/CoreFoundation/CFSocketInvalidate(_:))

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

[`CFSocketIsValid(_:)`](/documentation/CoreFoundation/CFSocketIsValid(_:))

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

[`CFSocketSendData(_:_:_:_:)`](/documentation/CoreFoundation/CFSocketSendData(_:_:_:_:))

Sends data over a CFSocket object.

### Callbacks

[`CFSocketCallBack`](/documentation/CoreFoundation/CFSocketCallBack)

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

### Data Types

[`CFSocketContext`](/documentation/CoreFoundation/CFSocketContext)

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

[`CFSocketNativeHandle`](/documentation/CoreFoundation/CFSocketNativeHandle)

Type for the platform-specific native socket handle.

[`CFSocketSignature`](/documentation/CoreFoundation/CFSocketSignature)

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

### Constants

[`CFSocketCallBackType`](/documentation/CoreFoundation/CFSocketCallBackType)

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

[CFSocket Flags](/documentation/CoreFoundation/1560944-cfsocket-flags)

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

[`CFSocketError`](/documentation/CoreFoundation/CFSocketError)

Error codes for many CFSocket functions.

## See Also

  [Threading Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html#//apple_ref/doc/uid/10000057i)

  [CFNetwork Programming Guide](https://developer.apple.com/library/archive/documentation/Networking/Conceptual/CFNetwork/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001132)



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)