Zen API

- Home
- Help Topics
- Types by Category
- All Types
- Files

All Classes Files Functions Variables Typedefs Macros Groups Pages
Public Member Functions
kSocket Class Reference
kApi/Io

## Description

Represents a network socket.

Inheritance diagram for kSocket:

## Public Member Functions
kAlloc | kObject_Alloc (kObject object)
| Gets the memory allocator associated with this object. More...
kStatus | kObject_Clone (kObject *object, kObject source, kAlloc allocator)
| Constructs a new object by copying an existing object, including any aggregated child elements. More...
kStatus | kObject_Destroy (kObject object)
| Destroys the object. More...
kStatus | kObject_Dispose (kObject object)
| Destroys the object and any aggregated child elements. More...
kBool | kObject_Equals (kObject object, kObject other)
| Determines whether the object is equal to another object. More...
kSize | kObject_HashCode (kObject object)
| Gets a hash code representing the state of this object. More...
kBool | kObject_Is (kObject object, kType type)
| Determines whether this object is an instance of the specified type. More...
kBool | kObject_IsShared (kObject object)
| Reports whether the object is currently shared (reference count greater than one). More...
kStatus | kObject_SetPool (kObject object, kObjectPool pool)
| Sets the object pool associated with this object. More...
kStatus | kObject_Share (kObject object)
| Increments the reference count associated with this object. More...
kSize | kObject_Size (kObject object)
| Estimates the memory consumed by this object, including any aggregated child elements. More...
kType | kObject_Type (kObject object)
| Returns the type of the object. More...
kStatus | kSocket_Accept (kSocket socket, kSocket *connection, kAlloc allocator)
| Blocks until an incoming connection request is accepted. More...
kStatus | kSocket_Bind (kSocket socket, kIpAddress address, k32u port)
| Binds the socket to a local IP address and/or port. More...
kStatus | kSocket_BindToDevice (kSocket socket, const kChar *interfaceName)
| Binds the socket to a specific network interface. More...
kStatus | kSocket_Connect (kSocket socket, kIpAddress address, k32u port, k64u timeout)
| Connects the socket to a remote end point. More...
kStatus | kSocket_Construct (kSocket *socket, kIpVersion ipVersion, kSocketType socketType, kAlloc allocator)
| Constructs a kSocket object. More...
kStatus | kSocket_EnableBroadcast (kSocket socket, kBool broadcast)
| Enables or disables datagram broadcasting. More...
kStatus | kSocket_EnableReuseAddress (kSocket socket, kBool reuse)
| Enables or disables reuse of a local end point within a short period of time. More...
kSocketEvent | kSocket_Events (kSocket socket)
| Gets the events detected during the most recent wait operation. More...
kStatus | kSocket_Listen (kSocket socket, kSize backlog)
| Places the socket into a listening state, to monitor for incoming connection requests. More...
kStatus | kSocket_LocalEndPoint (kSocket socket, kIpEndPoint *endPoint)
| Returns the local end point for a bound socket. More...
kStatus | kSocket_Read (kSocket socket, void *buffer, kSize size, kSize *read)
| Reads one or more bytes. More...
kStatus | kSocket_ReadFrom (kSocket socket, kIpEndPoint *endPoint, void *buffer, kSize size, kSize *read)
| Reads a datagram. More...
kStatus | kSocket_RemoteEndPoint (kSocket socket, kIpEndPoint *endPoint)
| Returns the remote end point for a connected socket. More...
kStatus | kSocket_SetBlocking (kSocket socket, kBool isBlocking)
| Determines whether the socket will block on read/write requests. More...
kStatus | kSocket_SetEvents (kSocket socket, kSocketEvent events)
| Sets the event types that a socket will wait on. More...
kStatus | kSocket_SetLingerTime (kSocket socket, k64u lingerTime)
| Specifies the duration that a TCP connection can remain open when the socket is closed in order to ensure that all outbound bytes are transmitted to the receiver. More...
kStatus | kSocket_SetNoDelay (kSocket socket, kBool noDelay)
| Can be used to disable the Nagle algorithm. More...
kStatus | kSocket_SetReadBuffer (kSocket socket, kSize size)
| Sets the size of the read buffer used by the underlying operating system. More...
kStatus | kSocket_SetReadTimeout (kSocket socket, k64u timeout)
| Sets the timeout duration for blocking read operations. More...
kStatus | kSocket_SetWriteBuffer (kSocket socket, kSize size)
| Sets the size of the write buffer used by the underlying operating system. More...
kStatus | kSocket_SetWriteTimeout (kSocket socket, k64u timeout)
| Sets the timeout duration for blocking write operations. More...
kStatus | kSocket_Status (kSocket socket)
| Reports any internal errors that will prevent success of future communication attempts. More...
kStatus | kSocket_Wait (kSocket socket, k64u timeout)
| Waits for a socket event. More...
kStatus | kSocket_WaitAny (const kSocket *sockets, kSize count, k64u timeout)
| Waits until an event occurs on one or more sockets. More...
kStatus | kSocket_Write (kSocket socket, const void *buffer, kSize size, kSize *written)
| Writes one or more bytes. More...
kStatus | kSocket_WriteTo (kSocket socket, kIpAddress address, k32u port, const void *buffer, kSize size)
| Sends a datagram. More...

## Member Function Documentation

kAlloc kObject_Alloc | ( | kObject | object | ) |
| inherited

Gets the memory allocator associated with this object.

Most objects are constructed with an allocator, which is used to allocate the memory required by the object. Objects retain a reference to this allocator to enable further allocations and to free memory when the object is destroyed.

Parameters

  object | Object.

Returns
  Memory allocator.

See also
  kAlloc, kObject_Alloc_

kStatus kObject_Clone | ( | kObject * | object,
|  | kObject | source,
|  | kAlloc | allocator
| ) |  |
| inherited

Constructs a new object by copying an existing object, including any aggregated child elements.

If the source object is an object collection (e.g. kArrayList<kString>), any aggregated child objects are also cloned. In this case, the kObject_Dispose method can be used to free the cloned collection and its associated elements.

This method will fail if the source object (or an aggregated child element) does not support cloning.

Parameters

  object | Receives the constructed object.
  source | Source object.
  allocator | Memory allocator (or kNULL for default).

Returns
  Operation status.

See also
  kObject_Dispose

kStatus kObject_Destroy | ( | kObject | object | ) |
| inherited

Destroys the object.

The kObject_Destroy method destroys the object itself and any resources that are owned by the object. See Object Destruction for more information.

When an object is destroyed (or disposed), its reference count is decremented. The object is only truly destroyed when the reference count reaches zero. See Reference Counting for more information.

Parameters

  object | Object (or kNULL).

Returns
  Operation status.

See also
  Object Destruction, Reference Counting

kStatus kObject_Dispose | ( | kObject | object | ) |
| inherited

Destroys the object and any aggregated child elements.

The kObject_Dispose method destroys the object itself, any resources that are owned by the object, and if the object represents a collection of objects, any child objects in the collection. See Object Destruction for more information.

When an object is destroyed (or disposed), its reference count is decremented. The object is only truly destroyed when the reference count reaches zero. See Reference Counting for more information.

Parameters

  object | Object (or kNULL).

Returns
  Operation status.

See also
  Object Destruction, Reference Counting

kBool kObject_Equals | ( | kObject | object,
|  | kObject | other
| ) |  |
| inherited

Determines whether the object is equal to another object.

By default, objects are compared by reference; objects are considered equal if the given handles refer to the same object instance. However, some classes override the Equals method to provide a more meaningful comparison (e.g. kString).

Parameters

  object | Object.
  other | Object for comparison.

Returns
  kTRUE if the objects are equal.

kSize kObject_HashCode | ( | kObject | object | ) |
| inherited

Gets a hash code representing the state of this object.

By default, objects return a hash code based on the object handle value. However, some classes override the HashCode method to provide a more useful hash (e.g. kString).

Parameters

  object | Object.

Returns
  Hash code.

kBool kObject_Is | ( | kObject | object,
|  | kType | type
| ) |  |
| inherited

Determines whether this object is an instance of the specified type.

This function compares the type of this object with the given type. An object is considered to be an instance of a given type if a) the type represents a class and this object inherits from (or instantiates) that class, or b) the type represents an interface and this object implements the interface.

Parameters

  object | Object.
  type | Type.

Returns
  kTRUE if the object is of the specified type; otherwise kFALSE.

See also
  kObject_Is_, kType_Is

kBool kObject_IsShared | ( | kObject | object | ) |
| inherited

Reports whether the object is currently shared (reference count greater than one).

Objects are initialized with a reference count of one. The kObject_Share method can be used to increment the reference count. The kObject_Destroy and kObject_Dispose methods decrease the reference count, and when the reference count reaches zero, the object is actually destroyed/disposed.

This method can be used to determine if the reference count of an object is currently greater than one.

This method is thread-safe.

Parameters

  object | Object.

Returns
  kTRUE if the object is shared; kFALSE otherwise.

See also
  Reference Counting, kObject_Share

kStatus kObject_SetPool | ( | kObject | object,
|  | kObjectPool | pool
| ) |  |
| inherited

Sets the object pool associated with this object.

Object pools can be used to implement custom lifecycle management. If an object has an assigned pool, then the kObjectPool_Reclaim method will be called just prior to destruction, to provide an opportunity for the object to be reclaimed.

Parameters

  object | Object.
  pool | Pool object (or kNULL to clear the pool assignment).

Returns
  Operation status.

kStatus kObject_Share | ( | kObject | object | ) |
| inherited

Increments the reference count associated with this object.

This method is thread-safe.

Parameters

  object | Object.

Returns
  Operation status.

See also
  reference-counting

kSize kObject_Size | ( | kObject | object | ) |
| inherited

Estimates the memory consumed by this object, including any aggregated child elements.

This method can be optionally overridden by kObject-derived classes to report the amount of memory consumed by an object. The default implementation reports only the size of the class instance (additional allocations performed by the class are excluded).

Parameters

  object | Object.

Returns
  Object size, in bytes.

kType kObject_Type | ( | kObject | object | ) |
| inherited

Returns the type of the object.

Each object is an instance of a specific class type. The type handle returned by this function can be used to learn about the class.

Parameters

  object | Object.

Returns
  Type.

See also
  kType, kObject_Type_

kStatus kSocket_Accept | ( | kSocket | socket,
|  | kSocket * | connection,
|  | kAlloc | allocator
| ) |  |

Blocks until an incoming connection request is accepted.

Parameters

  socket | A socket object in the listening state.
  connection | Receives a socket object representing the newly-established connection, or kNULL.
  allocator | Memory allocator (or kNULL for default).

Returns
  Operation status.

kStatus kSocket_Bind | ( | kSocket | socket,
|  | kIpAddress | address,
|  | k32u | port
| ) |  |

Binds the socket to a local IP address and/or port.

Parameters

  socket | Destination for the constructed object handle.
  address | A local IP address, or kIpAddress_Any().
  port | A local port number, or kIP_PORT_ANY.

Returns
  Operation status.

kStatus kSocket_BindToDevice | ( | kSocket | socket,
|  | const kChar * | interfaceName
| ) |  |

Binds the socket to a specific network interface.

This function was introduced to work around a UDP broadcast issue on QNX, and is not supported on all platforms.

Parameters

  socket | Socket object.
  interfaceName | Device name.

Returns
  Operation status.

kStatus kSocket_Connect | ( | kSocket | socket,
|  | kIpAddress | address,
|  | k32u | port,
|  | k64u | timeout
| ) |  |

Connects the socket to a remote end point.

A connection can be attempted only once per socket object.

Parameters

  socket | Socket object.
  address | The remote IP address.
  port | The remote port number.
  timeout | The timeout interval, in microseconds.

Returns
  Operation status.

kStatus kSocket_Construct | ( | kSocket * | socket,
|  | kIpVersion | ipVersion,
|  | kSocketType | socketType,
|  | kAlloc | allocator
| ) |  |

Constructs a kSocket object.

Note: Methods in the socket API are not thread-safe, unless otherwise noted.

Parameters

  socket | Destination for the constructed object handle.
  ipVersion | Internet Protocol version.
  socketType | The type of socket to create (i.e. TCP or UDP).
  allocator | Memory allocator (or kNULL for default).

Returns
  Operation status.

kStatus kSocket_EnableBroadcast | ( | kSocket | socket,
|  | kBool | broadcast
| ) |  |

Enables or disables datagram broadcasting.

This function is typically used in conjunction with a UDP socket that is bound to a local address. Sending to kIpAddress_BroacastV4() will broadcast a datagram on the subnet associated with the bound IPv4 address.

Parameters

  socket | Socket object.
  broadcast | kTRUE to enable broadcasts; kFALSE otherwise.

Returns
  Operation status.

kStatus kSocket_EnableReuseAddress | ( | kSocket | socket,
|  | kBool | reuse
| ) |  |

Enables or disables reuse of a local end point within a short period of time.

The option is typically used to allow a server to rebind to a local end point while a previous socket with the same local end point is in the TIME_WAIT state. This can be useful when a server must be stopped and started within a brief interval, but there is a small risk that packets with identical source/destination information could be misdirected to the new socket.

Parameters

  socket | Socket object.
  reuse | kTRUE to enable reuse of IP addresses; kFALSE otherwise.

Returns
  Operation status.

kSocketEvent kSocket_Events | ( | kSocket | socket | ) |

Gets the events detected during the most recent wait operation.

Parameters

  socket | Socket object.

Returns
  Detected events.

kStatus kSocket_Listen | ( | kSocket | socket,
|  | kSize | backlog
| ) |  |

Places the socket into a listening state, to monitor for incoming connection requests.

Parameters

  socket | Socket object.
  backlog | The maximum number of pending connection requests to enqueue.

Returns
  Operation status.

kStatus kSocket_LocalEndPoint | ( | kSocket | socket,
|  | kIpEndPoint * | endPoint
| ) |  |

Returns the local end point for a bound socket.

Parameters

  socket | Socket object.
  endPoint | Local end point.

Returns
  Operation status.

kStatus kSocket_Read | ( | kSocket | socket,
|  | void * | buffer,
|  | kSize | size,
|  | kSize * | read
| ) |  |

Reads one or more bytes.

In blocking mode, this function will block until at least one byte is received (or a read timeout occurs).

In non-blocking mode, this function will read at least one byte. kSocket_Wait should be used to determine when this function can be called successfully.

If the socket was closed by the remote peer, this function will return kERROR_CLOSED.

Parameters

  socket | Socket object.
  buffer | Buffer to receive bytes.
  size | The maximum number of bytes to read.
  read | The number of bytes that were read.

Returns
  Operation status.

kStatus kSocket_ReadFrom | ( | kSocket | socket,
|  | kIpEndPoint * | endPoint,
|  | void * | buffer,
|  | kSize | size,
|  | kSize * | read
| ) |  |

Reads a datagram.

In blocking mode, this function will block until a datagram is read (or a read timeout occurs).

In non-blocking mode, kSocket_Wait should be used to determine when this function can be called successfully.

Parameters

  socket | Socket object.
  endPoint | The address of the sender.
  buffer | Buffer to receive the datagram.
  size | The maximum number of bytes to return.
  read | The number of bytes that were read.

Returns
  Operation status.

kStatus kSocket_RemoteEndPoint | ( | kSocket | socket,
|  | kIpEndPoint * | endPoint
| ) |  |

Returns the remote end point for a connected socket.

Parameters

  socket | Socket object.
  endPoint | Remote end point.

Returns
  Operation status.

kStatus kSocket_SetBlocking | ( | kSocket | socket,
|  | kBool | isBlocking
| ) |  |

Determines whether the socket will block on read/write requests.

By default, sockets are created in blocking mode.

Parameters

  socket | Socket object.
  isBlocking | If kTRUE, the socket will be placed in blocking mode.

Returns
  Operation status.

kStatus kSocket_SetEvents | ( | kSocket | socket,
|  | kSocketEvent | events
| ) |  |

Sets the event types that a socket will wait on.

By default, sockets wait on read events.

Parameters

  socket | Socket object.
  events | One or more event types to wait on.

Returns
  Operation status.

kStatus kSocket_SetLingerTime | ( | kSocket | socket,
|  | k64u | lingerTime
| ) |  |

Specifies the duration that a TCP connection can remain open when the socket is closed in order to ensure that all outbound bytes are transmitted to the receiver.

If the linger time is not set, then a default linger time will be selected by the underlying operating system.

Parameters

  socket | Socket object.
  lingerTime | Linger time, in microseconds (0 for immediate closure).

Returns
  Operation status.

kStatus kSocket_SetNoDelay | ( | kSocket | socket,
|  | kBool | noDelay
| ) |  |

Can be used to disable the Nagle algorithm.

The Nagle algorithm is enabled by default. When enabled, small segments of outbound data are coalesced over a brief time period in order to improve network efficiency.

Parameters

  socket | Socket object.
  noDelay | kTRUE to disable the Nagle algorithm; kFALSE to enable.

Returns
  Operation status.

kStatus kSocket_SetReadBuffer | ( | kSocket | socket,
|  | kSize | size
| ) |  |

Sets the size of the read buffer used by the underlying operating system.

Parameters

  socket | Socket object.
  size | Size of the read buffer.

Returns
  Operation status.

kStatus kSocket_SetReadTimeout | ( | kSocket | socket,
|  | k64u | timeout
| ) |  |

Sets the timeout duration for blocking read operations.

By default, kSocket objects do not use a timeout interval and can block indefinitely.

Parameters

  socket | Socket object.
  timeout | Timeout value, in microseconds.

Returns
  Operation status.

kStatus kSocket_SetWriteBuffer | ( | kSocket | socket,
|  | kSize | size
| ) |  |

Sets the size of the write buffer used by the underlying operating system.

Parameters

  socket | Socket object.
  size | Size of the write buffer.

Returns
  Operation status.

kStatus kSocket_SetWriteTimeout | ( | kSocket | socket,
|  | k64u | timeout
| ) |  |

Sets the timeout duration for blocking write operations.

By default, socket objects do not use a timeout interval and can block indefinitely.

Parameters

  socket | Socket object.
  timeout | Timeout value, in microseconds.

Returns
  Operation status.

kStatus kSocket_Status | ( | kSocket | socket | ) |

Reports any internal errors that will prevent success of future communication attempts.

Parameters

  socket | Socket object.

Returns
  Socket status.

kStatus kSocket_Wait | ( | kSocket | socket,
|  | k64u | timeout
| ) |  |

Waits for a socket event.

Before calling this function, use kSocket_SetEvents to specify the events that the socket should wait on. After calling this function, use the kSocket_Events function to determine which events have occurred.

This function will return kERROR_TIMEOUT if no events occur by the end of the timeout period.

Parameters

  socket | Socket object.
  timeout | Timeout, in microseconds.

Returns
  Operation status.

kStatus kSocket_WaitAny | ( | const kSocket * | sockets,
|  | kSize | count,
|  | k64u | timeout
| ) |  |

Waits until an event occurs on one or more sockets.

Before calling this function, use kSocket_SetEvents to specify the events that the sockets should wait on. After calling this function, use the kSocket_Events function to determine which events have occurred.

This function will return kERROR_TIMEOUT if no socket events occur by the end of the timeout period.

Parameters

  sockets | An array of sockets to wait on.
  count | The number of sockets in the array.
  timeout | Timeout, in microseconds.

Returns
  Operation status.

kStatus kSocket_Write | ( | kSocket | socket,
|  | const void * | buffer,
|  | kSize | size,
|  | kSize * | written
| ) |  |

Writes one or more bytes.

In blocking mode, this function will block until all bytes are written (or a write timeout occurs).

In non-blocking mode, this function will write zero or more bytes. kSocket_Wait can be used to determine when buffer space is available for a write operation, increasing the odds that bytes can be written successfully.

Parameters

  socket | Socket object.
  buffer | Buffer of bytes to write.
  size | The number of bytes to write.
  written | The number of bytes that were written.

Returns
  Operation status.

kStatus kSocket_WriteTo | ( | kSocket | socket,
|  | kIpAddress | address,
|  | k32u | port,
|  | const void * | buffer,
|  | kSize | size
| ) |  |

Sends a datagram.

In blocking mode, this function will block until the datagram is sent (or a write timeout occurs).

In non-blocking mode, use kSocket_Wait to determine when this function can be called successfully.

Parameters

  socket | Socket object.
  address | IP address of the recipient.
  port | Port number of the recipient.
  buffer | Buffer containing the datagram to send.
  size | The number of bytes in the supplied buffer.

Returns
  Operation status.

The documentation for this class was generated from the following file:

- Io/kSocket.h