KNetwork::KServerSocket
#include <k3serversocket.h>

Signals | |
void | bound (const KNetwork::KResolverEntry &local) |
void | closed () |
void | gotError (int code) |
void | hostFound () |
void | readyAccept () |
Public Member Functions | |
KServerSocket (const QString &node, const QString &service, QObject *parent=nullptr) | |
KServerSocket (const QString &service, QObject *parent=nullptr) | |
KServerSocket (QObject *parent=nullptr) | |
~KServerSocket () override | |
KStreamSocket * | accept () override |
virtual bool | bind () |
bool | bind (const KResolverEntry &address) override |
virtual bool | bind (const QString &node, const QString &service) |
virtual bool | bind (const QString &service) |
void | close () override |
KSocketAddress | externalAddress () const override |
bool | listen (int backlog=5) override |
KSocketAddress | localAddress () const override |
virtual bool | lookup () |
KResolver & | resolver () const |
const KResolverResults & | resolverResults () const |
void | setAcceptBuffered (bool enable) |
void | setAddress (const QString &node, const QString &service) |
void | setAddress (const QString &service) |
void | setFamily (int families) |
void | setResolutionEnabled (bool enable) |
void | setTimeout (int msecs) |
![]() | |
QObject (QObject *parent) | |
bool | blockSignals (bool block) |
const QObjectList & | children () 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< QByteArray > | dynamicPropertyNames () const const |
virtual bool | event (QEvent *e) |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
T | findChild (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegExp ®Exp, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QString &name, 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 QMetaObject * | metaObject () const const |
void | moveToThread (QThread *targetThread) |
QString | objectName () const const |
void | objectNameChanged (const QString &objectName) |
QObject * | parent () 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 | |
T | qFindChild (const QObject *obj, const QString &name) |
QList< T > | qFindChildren (const QObject *obj, const QRegExp ®Exp) |
QList< T > | qFindChildren (const QObject *obj, const QString &name) |
T | qobject_cast (const QObject *object) |
T | qobject_cast (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) |
QThread * | thread () const const |
![]() | |
KPassiveSocketBase () | |
~KPassiveSocketBase () override | |
![]() | |
KSocketBase () | |
virtual | ~KSocketBase () |
bool | addressReuseable () const |
bool | blocking () const |
bool | broadcast () const |
SocketError | error () const |
QString | errorString () const |
bool | isIPv6Only () const |
QMutex * | mutex () 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) |
virtual void | setSocketDevice (KSocketDevice *device) |
KSocketDevice * | socketDevice () const |
Protected Member Functions | |
void | copyError () |
bool | setSocketOptions (int opts) override |
![]() | |
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 |
QObject * | sender () const const |
int | senderSignalIndex () const const |
virtual void | timerEvent (QTimerEvent *event) |
![]() | |
bool | hasDevice () const |
void | resetError () |
void | setError (SocketError error) |
virtual int | socketOptions () const |
Additional Inherited Members | |
![]() | |
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 } |
![]() | |
objectName | |
![]() | |
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 *context, Functor functor, 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) |
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, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method) |
bool | disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method) |
QString | tr (const char *sourceText, const char *disambiguation, int n) |
QString | trUtf8 (const char *sourceText, const char *disambiguation, int n) |
![]() | |
static QString | errorString (SocketError code) |
static bool | isFatalError (int code) |
![]() | |
typedef | QObjectList |
Detailed Description
A server socket for accepting connections.
This class provides functionality for creating a socket to listen for incoming connections and subsequently accept them.
To use this class, you must first set the parameters for the listening socket's address, then place it in listening mode.
A typical example would look like:
In this case, this class will place the socket into listening mode on the service pointed to by service
and will emit the readyAccept() signal when a connection is ready for accepting. The called slot is responsible for calling accept().
The location of the services file (where service
is looked up) is defined by _PATH_SERVICES in /usr/include/netdb.h. This is usually set to /etc/services. See RFC 1700 for more information on services. You can specify service
as a port number directly, rather than as a service name. This is discouraged as it prevents the end user from easily modifying the port number.
For another example of usage, this below code attempts to make a connection on any port within a range:
The called slot slotReadyAccept() is responsible for calling accept().
It is important to note that accept() can return either an object of type KNetwork::KStreamSocket or KNetwork::KBufferedSocket (default). If you want to accept a non-buffered socket, you must first call setAcceptBuffered.
- Warning
- If you use KServerSocket in an auxiliary (non-GUI) thread, you need to accept only KNetwork::KStreamSocket objects.
- Deprecated:
- Use KSocketFactory or KLocalSocket instead
Definition at line 109 of file k3serversocket.h.
Constructor & Destructor Documentation
◆ KServerSocket() [1/3]
KServerSocket::KServerSocket | ( | QObject * | parent = nullptr | ) |
Default constructor.
If the binding address isn't changed by setAddress, this socket will bind to all interfaces on this node and the port will be selected by the operating system.
- Parameters
-
parent the parent QObject object
Definition at line 60 of file k3serversocket.cpp.
◆ KServerSocket() [2/3]
Construct this object specifying the service to listen on.
If the binding address isn't changed by setAddress, this socket will bind to all interfaces and will listen on the port specified by service
. This is either a service name (e.g. 'www') or a port number (e.g. '80').
The location of the services file (where service
is looked up) is defined by _PATH_SERVICES in /usr/include/netdb.h. This is usually set to /etc/services. See RFC 1700 for more information on services.
- Parameters
-
service the service name to listen on parent the parent QObject object
Definition at line 67 of file k3serversocket.cpp.
◆ KServerSocket() [3/3]
KServerSocket::KServerSocket | ( | const QString & | node, |
const QString & | service, | ||
QObject * | parent = nullptr |
||
) |
Construct this object specifying the node and service names to listen on.
If the binding address isn't changed by setAddress, this socket will bind to the interface specified by node
and the port specified by service
. This is either a service name (e.g. 'www') or a port number (e.g. '80').
The location of the services file (where service
is looked up) is defined by _PATH_SERVICES in /usr/include/netdb.h. This is usually set to /etc/services. See RFC 1700 for more information on services.
- Parameters
-
node the node to bind to service the service port to listen on parent the parent QObject object
Definition at line 75 of file k3serversocket.cpp.
◆ ~KServerSocket()
|
override |
Destructor.
This will close the socket, if open.
Note, however, that accepted sockets do not get closed when this object closes.
Definition at line 84 of file k3serversocket.cpp.
Member Function Documentation
◆ accept()
|
overridevirtual |
Accepts one incoming connection and return the associated, open socket.
If this function cannot accept a new connection, it will return NULL. The specific object class returned by this function may vary according to the implementation: derived classes may return specialized objects descended from KStreamSocket.
- See also
- KBufferedSocket
- setAcceptBuffered
Implements KNetwork::KPassiveSocketBase.
Definition at line 292 of file k3serversocket.cpp.
◆ bind() [1/4]
|
virtual |
Binds the socket to the addresses previously set with setAddress().
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 213 of file k3serversocket.cpp.
◆ bind() [2/4]
|
overridevirtual |
Connect this socket to this specific address.
Reimplemented from KSocketBase.
Unlike bind(const QString&, const QString&) above, this function really does bind the socket. No lookup is performed. The bound() signal will be emitted.
Implements KNetwork::KPassiveSocketBase.
Definition at line 188 of file k3serversocket.cpp.
◆ bind() [3/4]
Binds this socket to the given nodename and service, or use the default ones if none are given.
Upon successful binding, the bound() signal will be emitted. If an error is found, the gotError() signal will be emitted.
This function returns true on success.
- Parameters
-
node the nodename service the service
Definition at line 201 of file k3serversocket.cpp.
◆ bind() [4/4]
|
virtual |
Binds the socket to the given service name.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
- Parameters
-
service the service
Definition at line 207 of file k3serversocket.cpp.
◆ bound
|
signal |
This signal is emitted when the socket successfully binds to an address.
- Parameters
-
local the local address we bound to
◆ close()
|
overridevirtual |
Closes this socket.
Implements KNetwork::KPassiveSocketBase.
Definition at line 277 of file k3serversocket.cpp.
◆ closed
|
signal |
This signal is emitted when the socket completes the closing/shut down process.
◆ copyError()
|
protected |
Convenience function to set this object's error code to match that of the socket device.
Definition at line 380 of file k3serversocket.cpp.
◆ externalAddress()
|
overridevirtual |
Returns this socket's externally-visible address if know.
Implements KNetwork::KPassiveSocketBase.
Definition at line 351 of file k3serversocket.cpp.
◆ gotError
|
signal |
This signal is emitted when this object finds an error.
The code
parameter contains the error code that can also be found by calling error().
◆ hostFound
|
signal |
This signal is emitted when the lookup is successfully completed.
◆ listen()
|
overridevirtual |
Puts this socket into listening mode.
Reimplemented from KPassiveSocketBase.
Placing a socket into listening mode means it will be able to receive incoming connections through the accept() method.
If you do not call this method but call accept() directly, the socket will be placed into listening mode automatically.
- Parameters
-
backlog the number of connection the system is to queue without accept() being called
- Returns
- true if the socket is now in listening mode.
Implements KNetwork::KPassiveSocketBase.
Definition at line 238 of file k3serversocket.cpp.
◆ localAddress()
|
overridevirtual |
Returns this socket's local address.
Implements KNetwork::KPassiveSocketBase.
Definition at line 346 of file k3serversocket.cpp.
◆ lookup()
|
virtual |
Starts the lookup for peer and local hostnames as well as their services.
If the blocking mode for this object is on, this function will wait for the lookup results to be available (by calling the KResolver::wait() method on the resolver objects).
When the lookup is done, the signal hostFound() will be emitted (only once, even if we're doing a double lookup). If the lookup failed (for any of the two lookups) the gotError() signal will be emitted with the appropriate error condition (see KSocketBase::SocketError).
This function returns true on success and false on error. Note that this is not the lookup result!
Definition at line 148 of file k3serversocket.cpp.
◆ readyAccept
|
signal |
This signal is emitted whenever the socket is ready for accepting – i.e., there is at least one connection waiting to be accepted.
◆ resolver()
KResolver & KServerSocket::resolver | ( | ) | const |
Returns the internal KResolver object used for looking up the host name and service.
This can be used to set extra options to the lookup process other than the default values, as well as obtaining the error codes in case of lookup failure.
Definition at line 99 of file k3serversocket.cpp.
◆ resolverResults()
const KResolverResults & KServerSocket::resolverResults | ( | ) | const |
Returns the internal list of resolved results for the binding address.
Definition at line 104 of file k3serversocket.cpp.
◆ setAcceptBuffered()
void KServerSocket::setAcceptBuffered | ( | bool | enable | ) |
Toggles whether the accepted socket will be buffered or not.
That is, the accept() function will always return a KStreamSocket object or descended from it. If buffering is enabled, the class to be returned will be KBufferedSocket.
By default, this flag is set to true.
- Parameters
-
enable whether to set the accepted socket to buffered mode
Definition at line 287 of file k3serversocket.cpp.
◆ setAddress() [1/2]
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Sets the address on which we will listen.
This will cause the socket to listen only on the interface given by node
and on the port given by service
. service
can either be a service name (e.g. 'www') or a port number (e.g. '80').
The location of the services file (where service
is looked up) is defined by _PATH_SERVICES in /usr/include/netdb.h. This is usually set to /etc/services. See RFC 1700 for more information on services.
- Parameters
-
node the node to bind to service the service port to listen on
Definition at line 133 of file k3serversocket.cpp.
◆ setAddress() [2/2]
void KServerSocket::setAddress | ( | const QString & | service | ) |
Sets the address on which we will listen.
The port to listen on is given by service
, and we will bind to all interfaces. To let the operating system choose a port, set the service to "0". service
can either be a service name (e.g. 'www') or a port number (e.g. '80').
The location of the services file (where service
is looked up) is defined by _PATH_SERVICES in /usr/include/netdb.h. This is usually set to /etc/services. See RFC 1700 for more information on services.
- Parameters
-
service the service name to listen on
Definition at line 123 of file k3serversocket.cpp.
◆ setFamily()
void KServerSocket::setFamily | ( | int | families | ) |
Sets the allowed families for the resolutions.
- Parameters
-
families the families that we want/accept
- See also
- KResolver::SocketFamilies for possible values
Definition at line 118 of file k3serversocket.cpp.
◆ setResolutionEnabled()
void KServerSocket::setResolutionEnabled | ( | bool | enable | ) |
Enables or disables name resolution.
If this flag is set to true, the bind() operation will trigger name lookup operations (i.e., converting a hostname into its binary form). If the flag is set to false, those operations will instead try to convert a string representation of an address without attempting name resolution.
This is useful, for instance, when IP addresses are in their string representation (such as "1.2.3.4") or come from other sources like KSocketAddress.
- Parameters
-
enable whether to enable
Definition at line 109 of file k3serversocket.cpp.
◆ setSocketOptions()
|
overrideprotectedvirtual |
Sets the socket options.
Reimplemented from KSocketBase.
Reimplemented from KNetwork::KSocketBase.
Definition at line 90 of file k3serversocket.cpp.
◆ setTimeout()
void KServerSocket::setTimeout | ( | int | msecs | ) |
Sets the timeout for accepting.
When you call accept(), it will wait at most msecs
milliseconds or return with an error (returning a NULL object).
- Parameters
-
msecs the time in milliseconds to wait, 0 to wait forever
Definition at line 143 of file k3serversocket.cpp.
The documentation for this class was generated from the following files:
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jan 29 2023 03:59:36 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.