NSSocketPort Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in OS X v10.0 and later.
Companion guide
Declared in
NSPort.h

Overview

NSSocketPort is a subclass of NSPort that represents a BSD socket. An NSSocketPort object can be used as an endpoint for distributed object connections. Companion classes, NSMachPort and NSMessagePort, allow for local (on the same machine) communication only. The NSSocketPort class allows for both local and remote communication, but may be more expensive than the others for the local case.

Tasks

Creating Instances

Getting Information

Instance Methods

address

Returns the receiver’s socket address structure.

- (NSData *)address
Return Value

The receiver’s socket address structure stored inside an NSData object.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPort.h

init

Initializes the receiver as a local TCP/IP socket of type SOCK_STREAM.

- (id)init
Return Value

An initialized local TCP/IP socket port of type SOCK_STREAM.

Discussion

The port number is selected by the system.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPort.h

initRemoteWithProtocolFamily:socketType:protocol:address:

Initializes the receiver as a remote socket with the provided arguments.

- (id)initRemoteWithProtocolFamily:(int)family socketType:(int)type protocol:(int)protocol address:(NSData *)address
Parameters
family

The protocol family for the socket port.

type

The type of socket.

protocol

The specific protocol to use from the the protocol family.

address

The family-specific socket address for the receiver copied into an NSData object.

Discussion

A connection is not opened to the remote address until data is sent.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPort.h

initRemoteWithTCPPort:host:

Initializes the receiver as a TCP/IP socket of type SOCK_STREAM that can connect to a remote host on a specified port.

- (id)initRemoteWithTCPPort:(unsigned short)port host:(NSString *)hostName
Parameters
port

The port to connect to.

hostName

The host name to connect to. hostName may be either a host name or an IPv4-style address.

Return Value

A TCP/IP socket port of type SOCK_STREAM that can connect to the remote host hostName on port port.

Discussion

A connection is not opened to the remote host until data is sent.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPort.h

initWithProtocolFamily:socketType:protocol:address:

Initializes the receiver as a local socket with the provided arguments.

- (id)initWithProtocolFamily:(int)family socketType:(int)type protocol:(int)protocol address:(NSData *)address
Parameters
family

The protocol family for the socket port.

type

The type of socket.

protocol

The specific protocol to use from the the protocol family.

address

The family-specific socket address for the receiver copied into an NSData object.

Return Value

A local socket port initialized with the provided arguments.

Discussion

The receiver must be added to a run loop before it can accept connections or receive messages. Incoming messages are passed to the receiver’s delegate method handlePortMessage:.

To create a standard TCP/IP socket, use initWithTCPPort:.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPort.h

initWithProtocolFamily:socketType:protocol:socket:

Initializes the receiver with a previously created local socket.

- (id)initWithProtocolFamily:(int)family socketType:(int)type protocol:(int)protocol socket:(NSSocketNativeHandle)sock
Parameters
family

The protocol family for the provided socket.

type

The type of the provided socket.

protocol

The specific protocol the provided socket uses.

sock

The previously created socket.

Return Value

A local socket port initialized with the provided socket.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPort.h

initWithTCPPort:

Initializes the receiver as a local TCP/IP socket of type SOCK_STREAM, listening on a specified port number.

- (id)initWithTCPPort:(unsigned short)port
Parameters
port

The port number for the newly created socket port to listen on. If port is 0, the system will assign a port number.

Return Value

An initialized local TCP/IP socket of type SOCK_STREAM, listening on port port.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPort.h

protocol

Returns the protocol that the receiver uses for communication.

- (int)protocol
Return Value

The protocol the receiver uses for communication.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPort.h

protocolFamily

Returns the protocol family that the receiver uses for communication.

- (int)protocolFamily
Return Value

The protocol family the receiver uses for communication.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPort.h

socket

Returns the receiver’s native socket identifier on the platform.

Return Value

The native socket identifier on the platform. For OS X, this is an integer file descriptor.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPort.h

socketType

Returns the receiver’s socket type.

- (int)socketType
Return Value

The receiver's socket type.

Availability
  • Available in OS X v10.0 and later.
Declared In
NSPort.h