Mac Developer Library

Developer

Foundation Framework Reference NSSocketPort Class Reference

Options
Deployment Target:

On This Page
Language:

NSSocketPort

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.

Inheritance


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in OS X v10.0 and later.
  • Initializes the receiver as a local TCP/IP socket of type SOCK_STREAM.

    Declaration

    Swift

    convenience init()

    Objective-C

    - (instancetype)init

    Return Value

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

    Discussion

    The port number is selected by the system.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

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

    Declaration

    Swift

    convenience init?(TCPPort port: UInt16)

    Objective-C

    - (instancetype)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.

    Discussion

    This method creates an IPv4 port, not an IPv6 port.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

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

    Declaration

    Swift

    init?(protocolFamily family: Int32, socketType type: Int32, `protocol` `protocol`: Int32, address address: NSData)

    Objective-C

    - (instancetype)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:.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • Initializes the receiver with a previously created local socket.

    Declaration

    Swift

    init?(protocolFamily family: Int32, socketType type: Int32, `protocol` `protocol`: Int32, socket sock: NSSocketNativeHandle)

    Objective-C

    - (instancetype)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.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

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

    Declaration

    Swift

    convenience init?(remoteWithTCPPort port: UInt16, host hostName: String?)

    Objective-C

    - (instancetype)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.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

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

    Declaration

    Swift

    init(remoteWithProtocolFamily family: Int32, socketType type: Int32, `protocol` `protocol`: Int32, address address: NSData)

    Objective-C

    - (instancetype)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.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • address address Property

    The receiver’s socket address structure stored inside an NSData object. (read-only)

    Declaration

    Swift

    @NSCopying var address: NSData { get }

    Objective-C

    @property(readonly, copy) NSData *address

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • protocol protocol Property

    The protocol that the receiver uses for communication. (read-only)

    Declaration

    Swift

    var `protocol`: Int32 { get }

    Objective-C

    @property(readonly) int protocol

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • The protocol family that the receiver uses for communication. (read-only)

    Declaration

    Swift

    var protocolFamily: Int32 { get }

    Objective-C

    @property(readonly) int protocolFamily

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • socket socket Property

    The receiver’s native socket identifier on the platform. (read-only)

    Declaration

    Swift

    var socket: NSSocketNativeHandle { get }

    Objective-C

    @property(readonly) NSSocketNativeHandle socket

    Discussion

    In OS X, the native socket identifier is an integer file descriptor.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.

  • The receiver’s socket type. (read-only)

    Declaration

    Swift

    var socketType: Int32 { get }

    Objective-C

    @property(readonly) int socketType

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.0 and later.