KNetwork::KStreamSocket Class Reference

`#include <kstreamsocket.h>`

Inheritance diagram for KNetwork::KStreamSocket:

## Signals

void | timedOut ()
Signals inherited from KNetwork::KClientSocketBase
void | stateChanged (int newstate)
void | gotError (int code)
void | hostFound ()
void | bound (const KResolverEntry &local)
void | aboutToConnect (const KResolverEntry &remote, bool &skip)
void | connected (const KResolverEntry &remote)
void | closed ()
void | readyRead ()
void | readyWrite ()

## Public Member Functions

| KStreamSocket (const QString &node=QString::null, const QString &service=QString::null, QObject *parent=0L, const char *name=0L)
virtual | ~KStreamSocket ()
int | timeout () const
int | remainingTimeout () const
void | setTimeout (int msecs)
virtual bool | bind (const QString &node=QString::null, const QString &service=QString::null)
virtual bool | bind (const KResolverEntry &entry)
virtual bool | connect (const QString &node=QString::null, const QString &service=QString::null)
virtual bool | connect (const KResolverEntry &entry)
Public Member Functions inherited from KNetwork::KClientSocketBase
| KClientSocketBase (QObject *parent, const char *name)
virtual | ~KClientSocketBase ()
SocketState | state () const
KResolver & | peerResolver () const
const KResolverResults & | peerResults () const
KResolver & | localResolver () const
const KResolverResults & | localResults () const
void | setResolutionEnabled (bool enable)
void | setFamily (int families)
virtual bool | lookup ()
void | connectToHost (const QString &host, Q_UINT16 port)
virtual bool | disconnect ()
virtual bool | open (int)
virtual void | close ()
virtual void | flush ()
virtual Q_LONG | bytesAvailable () const
virtual Q_LONG | waitForMore (int msecs, bool *timeout=0L)
virtual Q_LONG | readBlock (char *data, Q_ULONG maxlen)
virtual Q_LONG | readBlock (char *data, Q_ULONG maxlen, KSocketAddress &from)
virtual Q_LONG | peekBlock (char *data, Q_ULONG maxlen)
virtual Q_LONG | peekBlock (char *data, Q_ULONG maxlen, KSocketAddress &from)
virtual Q_LONG | writeBlock (const char *data, Q_ULONG len)
virtual Q_LONG | writeBlock (const char *data, Q_ULONG len, const KSocketAddress &to)
virtual KSocketAddress | localAddress () const
virtual KSocketAddress | peerAddress () const
bool | emitsReadyRead () const
virtual void | enableRead (bool enable)
bool | emitsReadyWrite () const
virtual void | enableWrite (bool enable)
Public Member Functions inherited from KNetwork::KActiveSocketBase
| KActiveSocketBase ()
virtual | ~KActiveSocketBase ()
virtual Offset | size () const
virtual Offset | at () const
virtual bool | at (Offset)
virtual bool | atEnd () const
virtual int | getch ()
virtual int | putch (int ch)
virtual int | ungetch (int)
Public Member Functions inherited from KNetwork::KSocketBase
| 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

## Friends

class | KServerSocket
class | KBufferedSocket

## Additional Inherited Members

Public Types inherited from KNetwork::KClientSocketBase
enum | SocketState { Idle, HostLookup, HostFound, Bound, Connecting, Open, Closing, Unconnected = Bound, Connected = Open, Connection = Open }
Public Types inherited from KNetwork::KSocketBase
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 }
Static Public Member Functions inherited from KNetwork::KSocketBase
static QString | errorString (SocketError code)
static bool | isFatalError (int code)
Protected Slots inherited from KNetwork::KClientSocketBase
virtual void | slotReadActivity ()
virtual void | slotWriteActivity ()
Protected Member Functions inherited from KNetwork::KClientSocketBase
virtual bool | setSocketOptions (int opts)
void | setState (SocketState state)
virtual void | stateChanging (SocketState newState)
void | copyError ()
Protected Member Functions inherited from KNetwork::KActiveSocketBase
void | setError (int status, SocketError error)
void | resetError ()
Protected Member Functions inherited from KNetwork::KSocketBase
virtual int | socketOptions () const
bool | hasDevice () const
void | setError (SocketError error)

## Detailed Description

Simple stream socket.

This class provides functionality to creating unbuffered, stream sockets. In the case of Internet (IP) sockets, this class creates and uses TCP/IP sockets.

Objects of this class start, by default, on non-blocking mode. Call setBlocking if you wish to change that.

KStreamSocket objects are thread-safe and can be used in auxiliary threads (i.e., not the thread in which the Qt event loop runs in). Note that KBufferedSocket cannot be used reliably in an auxiliary thread.

Sample usage:

QByteArray httpGet(const QString& hostname)
{
KStreamSocket socket(hostname, "http");
if (!socket.connect())
return QByteArray();
QByteArray data = socket.readAll();
return data;
}

Here's another sample, showing asynchronous operation:

DataRetriever::DataRetriever(const QString& hostname, const QString& port)
: socket(hostname, port)
{
// connect signals to our slots
QObject::connect(&socket, SIGNAL(connected(const KResolverEntry&)),
this, SLOT(slotSocketConnected()));
QObject::connect(&socket, SIGNAL(gotError(int)),
this, SLOT(slotSocketError(int)));
QObject::connect(&socket, SIGNAL(readyRead()),
this, SLOT(slotSocketReadyToRead()));
QObject::connect(&socket, SIGNAL(readyWrite()),
this, SLOT(slotSocketReadyToWrite()));
// set non-blocking mode in order to work asynchronously
socket.setBlocking(false);
// turn on signal emission
socket.enableRead(true);
socket.enableWrite(true);
// start connecting
socket.connect();
}

See also
  KNetwork::KBufferedSocket, KNetwork::KServerSocket

Author
  Thiago Macieira thiag.nosp@m.o@kd.nosp@m.e.org

Definition at line 97 of file kstreamsocket.h.

## Constructor & Destructor Documentation

KStreamSocket::KStreamSocket | ( | const QString & | node = `QString::null`,
|  | const QString & | service = `QString::null`,
|  | QObject * | parent = `0L`,
|  | const char * | name = `0L`
| ) |  |

Default constructor.

Parameters

  node | destination host
  service | destination service to connect to
  parent | the parent QObject object
  name | name for this object

Definition at line 53 of file kstreamsocket.cpp.

KStreamSocket::~KStreamSocket | ( |  | ) |

virtual

Destructor.

This closes the socket.

Definition at line 67 of file kstreamsocket.cpp.

## Member Function Documentation

bool KStreamSocket::bind | ( | const QString & | node = `QString::null`,
|  | const QString & | service = `QString::null`
| ) |  |

virtual

Binds this socket to the given nodename and service, or use the default ones if none are given.

In order to bind to a service and allow the operating system to choose the interface, set `node` to QString::null.

Reimplemented from KClientSocketBase.

Upon successful binding, the bound signal will be emitted. If an error is found, the gotError signal will be emitted.

Note
  Due to the internals of the name lookup and binding mechanism, some (if not most) implementations of this function do not actually bind the socket until the connection is requested (see connect). They only set the values for future reference.

This function returns true on success.

Parameters

  node | the nodename
  service | the service

Implements KNetwork::KClientSocketBase.

Definition at line 96 of file kstreamsocket.cpp.

virtual bool KNetwork::KStreamSocket::bind | ( | const KResolverEntry & | entry | ) |

inline virtual

Reimplemented from KClientSocketBase.

Connect this socket to this specific address.

Unlike bind(const QString&, const QString&) above, this function really does bind the socket. No lookup is performed. The bound signal will be emitted.

Reimplemented from KNetwork::KClientSocketBase.

Definition at line 175 of file kstreamsocket.h.

bool KStreamSocket::connect | ( | const QString & | node = `QString::null`,
|  | const QString & | service = `QString::null`
| ) |  |

virtual

Reimplemented from KClientSocketBase.

Attempts to connect to the these hostname and service, or use the default ones if none are given. If a connection attempt is already in progress, check on its state and set the error status (NoError, meaning the connection is completed, or InProgress).

If the blocking mode for this object is on, this function will only return when all the resolved peer addresses have been tried or when a connection is established.

Upon successfully connecting, the connected signal will be emitted. If an error is found, the gotError signal will be emitted.

This function also implements timeout handling.

Parameters

  node | the remote node to connect to
  service | the service on the remote node to connect to

Implements KNetwork::KClientSocketBase.

Definition at line 108 of file kstreamsocket.cpp.

bool KStreamSocket::connect | ( | const KResolverEntry & | entry | ) |

virtual

Unshadowing from KClientSocketBase.

Reimplemented from KNetwork::KClientSocketBase.

Definition at line 192 of file kstreamsocket.cpp.

int KStreamSocket::remainingTimeout | ( |  | ) | const

Retrieves the remaining timeout time (in milliseconds).

This value equals timeout() if there's no connection in progress.

Definition at line 78 of file kstreamsocket.cpp.

void KStreamSocket::setTimeout | ( | int | msecs | ) |

Sets the timeout value.

Setting this value while a connection attempt is in progress will reset the timer.

Please note that the timeout value is valid for the connection attempt only. No other operations are timed against this value – including the name lookup associated.

Parameters

  msecs | the timeout value in milliseconds

Definition at line 88 of file kstreamsocket.cpp.

void KNetwork::KStreamSocket::timedOut | ( |  | ) |

signal

This signal is emitted when a connection timeout occurs.

int KStreamSocket::timeout | ( |  | ) | const

Retrieves the timeout value (in milliseconds).

Definition at line 73 of file kstreamsocket.cpp.

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

- kstreamsocket.h
- kstreamsocket.cpp

KDE Home | KDE Accessibility Home | Description of Access Keys