# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# KNetwork::KSocketBase Class Reference

Basic socket functionality. More...

`#include <ksocketbase.h>`

Inheritance diagram for KNetwork::KSocketBase:

[legend]
List of all members.

## Public Types

enum | SocketOptions { Blocking = 0x01, AddressReuseable = 0x02, IPv6Only = 0x04, Keepalive = 0x08, Broadcast = 0x10 }
enum | SocketError { NoError = 0, LookupFailure, AddressInUse, AlreadyCreated, AlreadyBound, AlreadyConnected, NotConnected, NotBound, NotCreated, WouldBlock, ConnectionRefused, ConnectionTimedOut, InProgress, NetFailure, NotSupported, Timeout, UnknownError, RemotelyDisconnected }

## Public Member Functions

| KSocketBase ()
virtual | ~KSocketBase ()
virtual bool | setBlocking (bool enable)
bool | blocking () const
virtual bool | setAddressReuseable (bool enable)
bool | addressReuseable () const
virtual bool | setIPv6Only (bool enable)
bool | isIPv6Only () const
virtual bool | setBroadcast (bool enable)
bool | broadcast () const
KSocketDevice * | socketDevice () const
virtual void | setSocketDevice (KSocketDevice *device)
int | setRequestedCapabilities (int add, int remove=0)
SocketError | error () const
QString | errorString () const
QMutex * | mutex () const

## Static Public Member Functions

static QString | errorString (SocketError code)
static bool | isFatalError (int code)

## Protected Member Functions

virtual bool | setSocketOptions (int opts)
virtual int | socketOptions () const
bool | hasDevice () const
void | setError (SocketError error)

## Friends

class | KSocketDevice

## Detailed Description

Basic socket functionality.

This class provides the basic socket functionlity for descended classes. Socket classes are thread-safe and provide a recursive mutex should it be needed.

Note:
  This class is abstract.

Author:
  Thiago Macieira <thiago.macieira@kdemail.net>

Definition at line 97 of file ksocketbase.h.

## Member Enumeration Documentation

enum KNetwork::KSocketBase::SocketOptions

Possible socket options.

These are the options that may be set on a socket:

- Blocking: whether the socket shall operate in blocking or non-blocking mode. This flag defaults to on. See setBlocking.
- AddressReusable: whether the address used by this socket will be available for reuse by other sockets. This flag defaults to off. See setAddressReuseable.
- IPv6Only: whether an IPv6 socket will accept IPv4 connections through a mapped address. This flag defaults to off. See setIPv6Only.
- KeepAlive: whether TCP should send keepalive probes when a connection has gone idle for far too long.
- Broadcast: whether this socket is allowed to send broadcast packets and will receive packets sent to broadcast.

Definition at line 118 of file ksocketbase.h.

enum KNetwork::KSocketBase::SocketError

Possible socket error codes.

This is a list of possible error conditions that socket classes may be expected to find.

- NoError: no error has been detected
- LookupFailure: if a name lookup has failed
- AddressInUse: address is already in use
- AlreadyBound: cannot bind again
- AlreadyCreated: cannot recreate the socket
- NotBound: operation required socket to be bound and it isn't
- NotCreated: operation required socket to exist and it doesn't
- WouldBlock: requested I/O operation would block
- ConnectionRefused: connection actively refused
- ConnectionTimedOut: connection timed out
- InProgress: operation (connection) is already in progress
- NetFailure: a network failure occurred (no route, host down, host unreachable or similar)
- NotSupported: requested operation is not supported
- Timeout: a timed operation timed out
- UnknownError: an unknown/unexpected error has happened
- RemotelyDisconnected: when a connection is disconnected by the other end (since 3.4)

See also:
  error, errorString

Definition at line 152 of file ksocketbase.h.

## Constructor & Destructor Documentation

KSocketBase::KSocketBase | ( |  | ) |

| Default constructor. Definition at line 50 of file ksocketbase.cpp.

KSocketBase::~KSocketBase | ( |  | ) | `[virtual]`

| Destructor. Definition at line 59 of file ksocketbase.cpp.

## Member Function Documentation

bool KSocketBase::setSocketOptions | ( | int | opts | ) | `[protected, virtual]`

Set the given socket options.

The default implementation does nothing but store the mask internally. Descended classes must override this function to achieve functionality and must also call this implementation.

Parameters:

  | opts | a mask of SocketOptions or-ed bits of options to set or unset

Returns:
  true on success

Note:
  this function sets the options corresponding to the bits enabled in `opts` but will also unset the optiosn corresponding to the bits not set.

Reimplemented in KNetwork::KBufferedSocket, KNetwork::KClientSocketBase, KNetwork::KServerSocket, and KNetwork::KSocketDevice.

Definition at line 65 of file ksocketbase.cpp.

Referenced by setAddressReuseable(), setBlocking(), setBroadcast(), and setIPv6Only().

int KSocketBase::socketOptions | ( |  | ) | const`[protected, virtual]`

Retrieves the socket options that have been set.

The default implementation just retrieves the mask from an internal variable. Descended classes may choose to override this function to read the values from the operating system.

Returns:
  the mask of the options set

Definition at line 71 of file ksocketbase.cpp.

Referenced by addressReuseable(), blocking(), broadcast(), KNetwork::KSocketDevice::create(), isIPv6Only(), KNetwork::KStreamSocket::KStreamSocket(), setAddressReuseable(), setBlocking(), setBroadcast(), and setIPv6Only().

bool KSocketBase::setBlocking | ( | bool | enable | ) | `[virtual]`

Sets this socket's blocking mode.

In blocking operation, all I/O functions are susceptible to blocking -- i.e., will not return unless the I/O can be satisfied. In non-blocking operation, if the I/O would block, the function will return an error and set the corresponding error code.

The default implementation toggles the Blocking flag with the current socket options and calls setSocketOptions.

Parameters:

  | enable | whether to set this socket to blocking mode

Returns:
  whether setting this value was successful; it is NOT the final blocking mode.

Definition at line 76 of file ksocketbase.cpp.

References setSocketOptions(), and socketOptions().

Referenced by KNetwork::KStreamSocket::connect(), and KNetwork::KSocksSocketDevice::connect().

bool KSocketBase::blocking | ( |  | ) | const

Retrieves this socket's blocking mode.

Returns:
  true if this socket is/will be operated in blocking mode, false if non-blocking.

Definition at line 81 of file ksocketbase.cpp.

References socketOptions().

Referenced by KNetwork::KServerSocket::accept(), KNetwork::KServerSocket::bind(), KNetwork::KStreamSocket::connect(), KNetwork::KSocksSocketDevice::connect(), KNetwork::KServerSocket::lookup(), KNetwork::KClientSocketBase::lookup(), and KNetwork::KDatagramSocket::receive().

bool KSocketBase::setAddressReuseable | ( | bool | enable | ) | `[virtual]`

Sets this socket's address reuseable flag.

When the address reuseable flag is active, the address used by this socket is left reuseable for other sockets to bind. If the flag is not active, no other sockets may reuse the same address.

The default implementation toggles the AddressReuseable flag with the current socket options and calls setSocketOptions.

Parameters:

  | enable | whether to set the flag on or off

Returns:
  true if setting this flag was successful

Definition at line 86 of file ksocketbase.cpp.

References setSocketOptions(), and socketOptions().

bool KSocketBase::addressReuseable | ( |  | ) | const

Retrieves this socket's address reuseability flag.

Returns:
  true if this socket's address can be reused, false if it can't.

Definition at line 91 of file ksocketbase.cpp.

References socketOptions().

bool KSocketBase::setIPv6Only | ( | bool | enable | ) | `[virtual]`

Sets this socket's IPv6 Only flag.

When this flag is on, an IPv6 socket will only accept, connect, send to or receive from IPv6 addresses. When it is off, it will also talk to IPv4 addresses through v4-mapped addresses.

This option has no effect on non-IPv6 sockets.

The default implementation toggles the IPv6Only flag with the current socket options and calls setSocketOptions.

Parameters:

  | enable | whether to set the flag on or off

Returns:
  true if setting this flag was successful

Definition at line 96 of file ksocketbase.cpp.

References setSocketOptions(), and socketOptions().

bool KSocketBase::isIPv6Only | ( |  | ) | const

Retrieves this socket's IPv6 Only flag.

Returns:
  true if this socket will ignore IPv4-compatible and IPv4-mapped addresses, false if it will accept them.

Definition at line 101 of file ksocketbase.cpp.

References socketOptions().

bool KSocketBase::setBroadcast | ( | bool | enable | ) | `[virtual]`

Sets this socket Broadcast flag.

Datagram-oriented sockets cannot normally send packets to broadcast addresses, nor will they receive packets that were sent to a broadcast address. To do so, you need to enable the Broadcast flag.

This option has no effect on stream-oriented sockets.

Returns:
  true if setting this flag was successful.

Definition at line 106 of file ksocketbase.cpp.

References setSocketOptions(), and socketOptions().

bool KSocketBase::broadcast | ( |  | ) | const

Retrieves this socket's Broadcast flag.

Returns:
  true if this socket can send and receive broadcast packets, false if it can't.

Definition at line 111 of file ksocketbase.cpp.

References socketOptions().

KSocketDevice * KSocketBase::socketDevice | ( |  | ) | const

Retrieves the socket implementation used on this socket.

This function creates the device if none has been set using the default factory.

Definition at line 116 of file ksocketbase.cpp.

References mutex(), and setSocketDevice().

Referenced by KNetwork::KServerSocket::accept(), KNetwork::KServerSocket::bind(), KNetwork::KClientSocketBase::bind(), KNetwork::KClientSocketBase::bytesAvailable(), KNetwork::KServerSocket::close(), KNetwork::KClientSocketBase::close(), KNetwork::KBufferedSocket::close(), KNetwork::KStreamSocket::connect(), KNetwork::KClientSocketBase::connect(), KNetwork::KServerSocket::copyError(), KNetwork::KClientSocketBase::copyError(), KNetwork::KClientSocketBase::disconnect(), KNetwork::KClientSocketBase::enableRead(), KNetwork::KBufferedSocket::enableRead(), KNetwork::KClientSocketBase::enableWrite(), KNetwork::KBufferedSocket::enableWrite(), KNetwork::KServerSocket::externalAddress(), KNetwork::KServerSocket::localAddress(), KNetwork::KClientSocketBase::localAddress(), KNetwork::KClientSocketBase::peekBlock(), KNetwork::KClientSocketBase::peerAddress(), KNetwork::KClientSocketBase::readBlock(), KNetwork::KDatagramSocket::receive(), KNetwork::KServerSocket::setSocketOptions(), KNetwork::KClientSocketBase::setSocketOptions(), KNetwork::KBufferedSocket::slotReadActivity(), KNetwork::KBufferedSocket::slotWriteActivity(), KNetwork::KClientSocketBase::stateChanging(), KNetwork::KClientSocketBase::waitForMore(), KNetwork::KClientSocketBase::writeBlock(), and KNetwork::KBufferedSocket::writeBlock().

void KSocketBase::setSocketDevice | ( | KSocketDevice * | device | ) | `[virtual]`

Sets the socket implementation to be used on this socket.

Note: it is an error to set this if the socket device has already been set once.

This function is provided virtual so that derived classes can catch the setting of a device and properly set their own states and internal variables. The parent class must be called.

This function is called by socketDevice above when the socket is first created.

Reimplemented in KNetwork::KBufferedSocket.

Definition at line 136 of file ksocketbase.cpp.

References mutex().

Referenced by KNetwork::KServerSocket::accept(), KNetwork::KSocketDevice::KSocketDevice(), and socketDevice().

int KSocketBase::setRequestedCapabilities | ( | int | add,
|  | int | remove = `0`
| ) |

Sets the internally requested capabilities for a socket device.

Most socket classes can use any back-end implementation. However, a few may require specific capabilities not provided in the default implementation. By using this function, derived classes can request that a backend with those capabilities be created when necessary.

For the possible flags, see KSocketDevice::Capabilities. However, note that only the Can* flags make sense in this context.

Note:
  Since socketDevice must always return a valid backend object, it is is possible that the created device does not conform to all requirements requested. Implementations sensitive to this fact should test the object returned by socketDevice (through KSocketDevice::capabilities, for instance) the availability.

Parameters:

  | add | mask of KSocketDevice::Capabilities to add
  | remove | mask of bits to remove from the requirements

Returns:
  the current mask of requested capabilities

Definition at line 143 of file ksocketbase.cpp.

bool KSocketBase::hasDevice | ( |  | ) | const`[protected]`

| Returns true if the socket device has been initialised in this object, either by calling socketDevice() or setSocketDevice. Definition at line 150 of file ksocketbase.cpp. Referenced by KNetwork::KClientSocketBase::setSocketOptions().

void KSocketBase::setError | ( | SocketError | error | ) | `[protected]`

Sets the socket's error code.

Parameters:

  | error | the error code

Definition at line 155 of file ksocketbase.cpp.

Referenced by KNetwork::KServerSocket::accept(), KNetwork::KServerSocket::bind(), KNetwork::KServerSocket::copyError(), and KNetwork::KServerSocket::lookup().

KSocketBase::SocketError KSocketBase::error | ( |  | ) | const

Retrieves the socket error code.

See also:
  errorString

Definition at line 160 of file ksocketbase.cpp.

Referenced by KNetwork::KStreamSocket::connect(), KNetwork::KClientSocketBase::connect(), KNetwork::KServerSocket::copyError(), KNetwork::KClientSocketBase::copyError(), KNetwork::KClientSocketBase::peekBlock(), KNetwork::KClientSocketBase::readBlock(), KNetwork::KBufferedSocket::slotReadActivity(), KNetwork::KBufferedSocket::slotWriteActivity(), KNetwork::KClientSocketBase::waitForMore(), and KNetwork::KClientSocketBase::writeBlock().

QString KNetwork::KSocketBase::errorString | ( |  | ) | const`[inline]`

| Returns the error string corresponding to this error condition. Definition at line 383 of file ksocketbase.h.

QMutex * KSocketBase::mutex | ( |  | ) | const

Returns the internal mutex for this class.

Note on multithreaded use of sockets: the socket classes are thread-safe by design, but you should be aware of problems regarding socket creation, connection and destruction in multi-threaded programs. The classes are guaranteed to work while the socket exists, but it's not wise to call connect in multiple threads.

Also, this mutex must be unlocked before the object is destroyed, which means you cannot use it to guard against other threads accessing the object while destroying it. You must ensure there are no further references to this object when deleting it.

Definition at line 278 of file ksocketbase.cpp.

Referenced by KNetwork::KClientSocketBase::enableRead(), KNetwork::KClientSocketBase::enableWrite(), KNetwork::KSocketDevice::exceptionNotifier(), KNetwork::KSocketDevice::readNotifier(), KNetwork::KBufferedSocket::setInputBuffering(), KNetwork::KBufferedSocket::setOutputBuffering(), setSocketDevice(), KNetwork::KSocketDevice::setSocketOptions(), KNetwork::KServerSocket::setSocketOptions(), KNetwork::KClientSocketBase::setSocketOptions(), KNetwork::KBufferedSocket::slotReadActivity(), KNetwork::KBufferedSocket::slotWriteActivity(), socketDevice(), and KNetwork::KSocketDevice::writeNotifier().

QString KSocketBase::errorString | ( | SocketError | code | ) | `[static]`

Returns the string describing the given error code, i18n'ed.

Parameters:

  | code | the error code

Definition at line 166 of file ksocketbase.cpp.

bool KSocketBase::isFatalError | ( | int | code | ) | `[static]`

Returns true if the given error code is a fatal one, false otherwise.

The parameter here is of type int so that casting isn't necessary when using the parameter to signal QClientSocketBase::gotError.

Parameters:

  | code | the code to test

Definition at line 259 of file ksocketbase.cpp.

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

- ksocketbase.h
- ksocketbase.cpp

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:19:26 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003