kdecore Library API Documentation

KNetwork::KServerSocket Class Reference

A server socket for accepting connections. More...

#include <kserversocket.h>

Inheritance diagram for KNetwork::KServerSocket:

QObject KNetwork::KPassiveSocketBase KNetwork::KSocketBase List of all members.

Signals

void gotError (int code)
void hostFound ()
void bound (const KResolverEntry &local)
void closed ()
void readyAccept ()

Public Member Functions

 KServerSocket (QObject *parent=0L, const char *name=0L)
 KServerSocket (const QString &service, QObject *parent=0L, const char *name=0L)
 KServerSocket (const QString &node, const QString &service, QObject *parent=0L, const char *name=0L)
 ~KServerSocket ()
KResolverresolver () const
const KResolverResultsresolverResults () const
void setResolutionEnabled (bool enable)
void setFamily (int families)
void setAddress (const QString &service)
void setAddress (const QString &node, const QString &service)
void setTimeout (int msecs)
virtual bool lookup ()
virtual bool bind (const QString &node, const QString &service)
virtual bool bind (const QString &service)
virtual bool bind ()
virtual bool bind (const KResolverEntry &address)
virtual bool listen (int backlog=5)
virtual void close ()
void setAcceptBuffered (bool enable)
virtual KActiveSocketBaseaccept ()
virtual KSocketAddress localAddress () const
virtual KSocketAddress externalAddress () const

Protected Member Functions

virtual bool setSocketOptions (int opts)
void copyError ()

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:

KServerSocket *ss = new KServerSocket(service); QObject::connect(ss, SIGNAL(readyAccept()), this, SLOT(slotReadyAccept())) ss->listen();

In that 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.

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

Definition at line 63 of file kserversocket.h.


Constructor & Destructor Documentation

KNetwork::KServerSocket::KServerSocket QObject parent = 0L,
const char *  name = 0L
 

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
name the name of this object

KNetwork::KServerSocket::KServerSocket const QString service,
QObject parent = 0L,
const char *  name = 0L
 

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.

Parameters:
service the service name to listen on
parent the parent QObject object
name the name of this object

KNetwork::KServerSocket::KServerSocket const QString node,
const QString service,
QObject parent = 0L,
const char *  name = 0L
 

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.

Parameters:
node the node to bind to
service the service port to listen on
parent the parent QObject object
name the name of this object

KNetwork::KServerSocket::~KServerSocket  ) 
 

Destructor.

This will close the socket, if open.

Note, however, that accepted sockets do not get closed when this object closes.


Member Function Documentation

virtual bool KNetwork::KServerSocket::setSocketOptions int  opts  )  [protected, virtual]
 

Sets the socket options.

Reimplemented from KSocketBase.

Reimplemented from KNetwork::KSocketBase.

KResolver& KNetwork::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.

const KResolverResults& KNetwork::KServerSocket::resolverResults  )  const
 

Returns the internal list of resolved results for the binding address.

void KNetwork::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

void KNetwork::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

void KNetwork::KServerSocket::setAddress const QString service  ) 
 

Sets the address on which we will listen.

The port to listen on is given by and we will bind to all interfaces. To let the operating system choose a port, set the service to "0".

Parameters:
service the service name to listen on

void KNetwork::KServerSocket::setAddress const QString node,
const QString service
 

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.

Parameters:
node the node to bind to
service the service port to listen on

void KNetwork::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

virtual bool KNetwork::KServerSocket::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!

virtual bool KNetwork::KServerSocket::bind const QString node,
const QString service
[virtual]
 

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

virtual bool KNetwork::KServerSocket::bind const QString service  )  [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

virtual bool KNetwork::KServerSocket::bind  )  [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.

virtual bool KNetwork::KServerSocket::bind const KResolverEntry address  )  [virtual]
 

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.

virtual bool KNetwork::KServerSocket::listen int  backlog = 5  )  [virtual]
 

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.

virtual void KNetwork::KServerSocket::close  )  [virtual]
 

Closes this socket.

Implements KNetwork::KPassiveSocketBase.

void KNetwork::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

virtual KActiveSocketBase* KNetwork::KServerSocket::accept  )  [virtual]
 

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 specialised objects descended from KStreamSocket.

Note:
This function should return a KStreamSocket object, but compiler deficiencies prevent such an adjustment. Therefore, we return the base class for active sockets, but it is guaranteed that the object will be a KStreamSocket or derived from it.
See also:
KBufferedSocket

setAcceptBuffered

Implements KNetwork::KPassiveSocketBase.

virtual KSocketAddress KNetwork::KServerSocket::localAddress  )  const [virtual]
 

Returns this socket's local address.

Implements KNetwork::KPassiveSocketBase.

virtual KSocketAddress KNetwork::KServerSocket::externalAddress  )  const [virtual]
 

Returns this socket's externally-visible address if know.

Implements KNetwork::KPassiveSocketBase.

void KNetwork::KServerSocket::gotError int  code  )  [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.

void KNetwork::KServerSocket::hostFound  )  [signal]
 

This signal is emitted when the lookup is successfully completed.

void KNetwork::KServerSocket::bound const KResolverEntry local  )  [signal]
 

This signal is emitted when the socket successfully binds to an address.

Parameters:
local the local address we bound to

void KNetwork::KServerSocket::closed  )  [signal]
 

This signal is emitted when the socket completes the closing/shut down process.

void KNetwork::KServerSocket::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.

void KNetwork::KServerSocket::copyError  )  [protected]
 

Convenience function to set this object's error code to match that of the socket device.


The documentation for this class was generated from the following file:
KDE Logo
This file is part of the documentation for kdecore Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Jul 22 10:16:30 2005 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003