KNetwork::KStreamSocket

KNetwork::KStreamSocket Class Reference

#include <k3streamsocket.h>

Inheritance diagram for KNetwork::KStreamSocket:

Signals

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

Public Member Functions

 KStreamSocket (const QString &node=QString(), const QString &service=QString(), QObject *parent=nullptr)
 
virtual ~KStreamSocket ()
 
virtual bool bind (const QString &node=QString(), const QString &service=QString()) override
 
bool bind (const KResolverEntry &entry) override
 
virtual bool connect (const QString &node=QString(), const QString &service=QString(), OpenMode mode=ReadWrite) override
 
virtual bool connect (const KResolverEntry &entry, OpenMode mode=ReadWrite) override
 
int remainingTimeout () const
 
void setTimeout (int msecs)
 
int timeout () const
 
- Public Member Functions inherited from KNetwork::KClientSocketBase
 KClientSocketBase (QObject *parent)
 
virtual ~KClientSocketBase ()
 
qint64 bytesAvailable () const override
 
void close () override
 
bool disconnect () override
 
bool emitsReadyRead () const
 
bool emitsReadyWrite () const
 
virtual void enableRead (bool enable)
 
virtual void enableWrite (bool enable)
 
virtual bool flush ()
 
KSocketAddress localAddress () const override
 
KResolverlocalResolver () const
 
const KResolverResultslocalResults () const
 
virtual bool lookup ()
 
bool open (OpenMode mode) override
 
KSocketAddress peerAddress () const override
 
KResolverpeerResolver () const
 
const KResolverResultspeerResults () const
 
void setFamily (int families)
 
void setResolutionEnabled (bool enable)
 
SocketState state () const
 
qint64 waitForMore (int msecs, bool *timeout=nullptr) override
 
- Public Member Functions inherited from KNetwork::KActiveSocketBase
 KActiveSocketBase (QObject *parent)
 
virtual ~KActiveSocketBase ()
 
bool atEnd () const override
 
QString errorString () const
 
bool isSequential () const override
 
bool open (OpenMode mode) override
 
qint64 peek (char *data, qint64 maxlen)
 
qint64 peek (char *data, qint64 maxlen, KSocketAddress &from)
 
qint64 pos () const override
 
qint64 read (char *data, qint64 maxlen)
 
QByteArray read (qint64 len)
 
qint64 read (char *data, qint64 maxlen, KSocketAddress &from)
 
bool seek (qint64) override
 
void setSocketDevice (KSocketDevice *device) override
 
qint64 size () const override
 
void ungetChar (char)
 
qint64 write (const char *data, qint64 len)
 
qint64 write (const QByteArray &data)
 
qint64 write (const char *data, qint64 len, const KSocketAddress &to)
 
- Public Member Functions inherited from QIODevice
 QIODevice (QObject *parent)
 
void aboutToClose ()
 
virtual qint64 bytesToWrite () const const
 
void bytesWritten (qint64 bytes)
 
virtual bool canReadLine () const const
 
void channelBytesWritten (int channel, qint64 bytes)
 
void channelReadyRead (int channel)
 
void commitTransaction ()
 
int currentReadChannel () const const
 
int currentWriteChannel () const const
 
QString errorString () const const
 
bool getChar (char *c)
 
bool isOpen () const const
 
bool isReadable () const const
 
bool isTextModeEnabled () const const
 
bool isTransactionStarted () const const
 
bool isWritable () const const
 
virtual bool open (QIODevice::OpenMode mode)
 
QIODevice::OpenMode openMode () const const
 
qint64 peek (char *data, qint64 maxSize)
 
QByteArray peek (qint64 maxSize)
 
bool putChar (char c)
 
qint64 read (char *data, qint64 maxSize)
 
QByteArray read (qint64 maxSize)
 
QByteArray readAll ()
 
int readChannelCount () const const
 
void readChannelFinished ()
 
QByteArray readLine (qint64 maxSize)
 
qint64 readLine (char *data, qint64 maxSize)
 
void readyRead ()
 
virtual bool reset ()
 
void rollbackTransaction ()
 
void setCurrentReadChannel (int channel)
 
void setCurrentWriteChannel (int channel)
 
void setTextModeEnabled (bool enabled)
 
qint64 skip (qint64 maxSize)
 
void startTransaction ()
 
void ungetChar (char c)
 
virtual bool waitForBytesWritten (int msecs)
 
virtual bool waitForReadyRead (int msecs)
 
qint64 write (const char *data)
 
qint64 write (const QByteArray &byteArray)
 
qint64 write (const char *data, qint64 maxSize)
 
int writeChannelCount () const const
 
- Public Member Functions inherited from QObject
 QObject (QObject *parent)
 
bool blockSignals (bool block)
 
const QObjectListchildren () const const
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
void deleteLater ()
 
void destroyed (QObject *obj)
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectInfo ()
 
void dumpObjectInfo () const const
 
void dumpObjectTree ()
 
void dumpObjectTree () const const
 
QList< QByteArraydynamicPropertyNames () const const
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
bool inherits (const char *className) const const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
void killTimer (int id)
 
virtual const QMetaObjectmetaObject () const const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const const
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
QVariant property (const char *name) const const
 
 Q_CLASSINFO (Name, Value)
 
 Q_DISABLE_COPY (Class)
 
 Q_DISABLE_COPY_MOVE (Class)
 
 Q_DISABLE_MOVE (Class)
 
 Q_EMIT Q_EMIT
 
 Q_ENUM (...)
 
 Q_ENUM_NS (...)
 
 Q_ENUMS (...)
 
 Q_FLAG (...)
 
 Q_FLAG_NS (...)
 
 Q_FLAGS (...)
 
 Q_GADGET Q_GADGET
 
 Q_INTERFACES (...)
 
 Q_INVOKABLE Q_INVOKABLE
 
 Q_NAMESPACE Q_NAMESPACE
 
 Q_NAMESPACE_EXPORT (EXPORT_MACRO)
 
 Q_OBJECT Q_OBJECT
 
 Q_PROPERTY (...)
 
 Q_REVISION Q_REVISION
 
 Q_SET_OBJECT_NAME (Object)
 
 Q_SIGNAL Q_SIGNAL
 
 Q_SIGNALS Q_SIGNALS
 
 Q_SLOT Q_SLOT
 
 Q_SLOTS Q_SLOTS
 
qFindChild (const QObject *obj, const QString &name)
 
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
 
QList< T > qFindChildren (const QObject *obj, const QString &name)
 
qobject_cast (QObject *object)
 
qobject_cast (const QObject *object)
 
 QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const char *name, const QVariant &value)
 
bool signalsBlocked () const const
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
QThreadthread () const const
 
- Public Member Functions inherited from KNetwork::KSocketBase
 KSocketBase ()
 
virtual ~KSocketBase ()
 
bool addressReuseable () const
 
bool blocking () const
 
bool broadcast () const
 
SocketError error () const
 
QString errorString () const
 
bool isIPv6Only () const
 
QMutexmutex () const
 
bool noDelay () const
 
virtual bool setAddressReuseable (bool enable)
 
virtual bool setBlocking (bool enable)
 
virtual bool setBroadcast (bool enable)
 
virtual bool setIPv6Only (bool enable)
 
virtual bool setNoDelay (bool enable)
 
int setRequestedCapabilities (int add, int remove=0)
 
KSocketDevicesocketDevice () const
 

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 QIODevice
enum  OpenModeFlag
 
- Public Types inherited from KNetwork::KSocketBase
enum  SocketError {
  NoError = 0, LookupFailure, AddressInUse, AlreadyCreated,
  AlreadyBound, AlreadyConnected, NotConnected, NotBound,
  NotCreated, WouldBlock, ConnectionRefused, ConnectionTimedOut,
  InProgress, NetFailure, NotSupported, Timeout,
  UnknownError, RemotelyDisconnected
}
 
enum  SocketOptions {
  Blocking = 0x01, AddressReuseable = 0x02, IPv6Only = 0x04, Keepalive = 0x08,
  Broadcast = 0x10, NoDelay = 0x20
}
 
- Properties inherited from QObject
 objectName
 
- Static Public Member Functions inherited from QObject
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
- Static Public Member Functions inherited from KNetwork::KSocketBase
static QString errorString (SocketError code)
 
static bool isFatalError (int code)
 
- Public Attributes inherited from QIODevice
 Append
 
 ExistingOnly
 
 NewOnly
 
 NotOpen
 
typedef OpenMode
 
 ReadOnly
 
 ReadWrite
 
 Text
 
 Truncate
 
 Unbuffered
 
 WriteOnly
 
- Public Attributes inherited from QObject
typedef QObjectList
 
- Protected Slots inherited from KNetwork::KClientSocketBase
virtual void slotReadActivity ()
 
virtual void slotWriteActivity ()
 
- Protected Member Functions inherited from KNetwork::KClientSocketBase
void copyError ()
 
qint64 peekData (char *data, qint64 maxlen, KSocketAddress *from) override
 
qint64 readData (char *data, qint64 maxlen, KSocketAddress *from) override
 
bool setSocketOptions (int opts) override
 
void setState (SocketState state)
 
virtual void stateChanging (SocketState newState)
 
qint64 writeData (const char *data, qint64 len, const KSocketAddress *to) override
 
- Protected Member Functions inherited from KNetwork::KActiveSocketBase
qint64 readData (char *data, qint64 len) override
 
void resetError ()
 
void setError (SocketError error)
 
qint64 writeData (const char *data, qint64 len) override
 
- Protected Member Functions inherited from QIODevice
virtual qint64 readLineData (char *data, qint64 maxSize)
 
void setErrorString (const QString &str)
 
void setOpenMode (QIODevice::OpenMode openMode)
 
- Protected Member Functions inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
int receivers (const char *signal) const const
 
QObjectsender () const const
 
int senderSignalIndex () const const
 
virtual void timerEvent (QTimerEvent *event)
 
- Protected Member Functions inherited from KNetwork::KSocketBase
bool hasDevice () const
 
void resetError ()
 
void setError (SocketError error)
 
virtual int socketOptions () const
 

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 KNetwork::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[email protected][email protected][email protected]e.org
Version
0.9
Deprecated:
Use KSocketFactory or KLocalSocket instead

Definition at line 99 of file k3streamsocket.h.

Constructor & Destructor Documentation

KStreamSocket::KStreamSocket ( const QString node = QString(),
const QString service = QString(),
QObject parent = nullptr 
)
explicit

Default constructor.

Parameters
nodedestination host
servicedestination service to connect to
parentthe parent QObject object

Definition at line 55 of file k3streamsocket.cpp.

KStreamSocket::~KStreamSocket ( )
virtual

Destructor.

This closes the socket.

Definition at line 69 of file k3streamsocket.cpp.

Member Function Documentation

bool KStreamSocket::bind ( const QString node = QString(),
const QString service = QString() 
)
overridevirtual

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

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
nodethe nodename
servicethe service

Implements KNetwork::KClientSocketBase.

Definition at line 101 of file k3streamsocket.cpp.

bool KStreamSocket::bind ( const KResolverEntry entry)
overridevirtual

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 116 of file k3streamsocket.cpp.

bool KStreamSocket::connect ( const QString node = QString(),
const QString service = QString(),
OpenMode  mode = ReadWrite 
)
overridevirtual

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
nodethe remote node to connect to
servicethe service on the remote node to connect to
modemode to operate this socket in

Implements KNetwork::KClientSocketBase.

Definition at line 121 of file k3streamsocket.cpp.

bool KStreamSocket::connect ( const KResolverEntry entry,
OpenMode  mode = ReadWrite 
)
overridevirtual

Unshadowing from KClientSocketBase.

Reimplemented from KNetwork::KClientSocketBase.

Definition at line 206 of file k3streamsocket.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 80 of file k3streamsocket.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
msecsthe timeout value in milliseconds

Definition at line 92 of file k3streamsocket.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 75 of file k3streamsocket.cpp.


The documentation for this class was generated from the following files:
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 10 2020 23:02:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.