.TH "KNetwork::KServerSocket" kde3 "11 Jun 2005" "Version Version 3.4.1" "kdecore Library" \" -*- nroff -*-
.ad l
.nh
.SH NAME
KNetwork::KServerSocket \- A server socket for accepting connections.  

.PP
.SH SYNOPSIS
.br
.PP
\fC#include <kserversocket.h>\fP
.PP
Inherits \fBQObject\fP, and \fBKNetwork::KPassiveSocketBase\fP.
.PP
.SS "Signals"

.in +1c
.ti -1c
.RI "void \fBgotError\fP (int code)"
.br
.ti -1c
.RI "void \fBhostFound\fP ()"
.br
.ti -1c
.RI "void \fBbound\fP (const \fBKResolverEntry\fP &local)"
.br
.ti -1c
.RI "void \fBclosed\fP ()"
.br
.ti -1c
.RI "void \fBreadyAccept\fP ()"
.br
.in -1c
.SS "Public Member Functions"

.in +1c
.ti -1c
.RI "\fBKServerSocket\fP (\fBQObject\fP *\fBparent\fP=0L, const char *\fBname\fP=0L)"
.br
.ti -1c
.RI "\fBKServerSocket\fP (const \fBQString\fP &service, \fBQObject\fP *\fBparent\fP=0L, const char *\fBname\fP=0L)"
.br
.ti -1c
.RI "\fBKServerSocket\fP (const \fBQString\fP &node, const \fBQString\fP &service, \fBQObject\fP *\fBparent\fP=0L, const char *\fBname\fP=0L)"
.br
.ti -1c
.RI "\fB~KServerSocket\fP ()"
.br
.ti -1c
.RI "\fBKResolver\fP & \fBresolver\fP () const "
.br
.ti -1c
.RI "const \fBKResolverResults\fP & \fBresolverResults\fP () const "
.br
.ti -1c
.RI "void \fBsetResolutionEnabled\fP (bool enable)"
.br
.ti -1c
.RI "void \fBsetFamily\fP (int families)"
.br
.ti -1c
.RI "void \fBsetAddress\fP (const \fBQString\fP &service)"
.br
.ti -1c
.RI "void \fBsetAddress\fP (const \fBQString\fP &node, const \fBQString\fP &service)"
.br
.ti -1c
.RI "void \fBsetTimeout\fP (int msecs)"
.br
.ti -1c
.RI "virtual bool \fBlookup\fP ()"
.br
.ti -1c
.RI "virtual bool \fBbind\fP (const \fBQString\fP &node, const \fBQString\fP &service)"
.br
.ti -1c
.RI "virtual bool \fBbind\fP (const \fBQString\fP &service)"
.br
.ti -1c
.RI "virtual bool \fBbind\fP ()"
.br
.ti -1c
.RI "virtual bool \fBbind\fP (const \fBKResolverEntry\fP &address)"
.br
.ti -1c
.RI "virtual bool \fBlisten\fP (int backlog=5)"
.br
.ti -1c
.RI "virtual void \fBclose\fP ()"
.br
.ti -1c
.RI "void \fBsetAcceptBuffered\fP (bool enable)"
.br
.ti -1c
.RI "virtual \fBKActiveSocketBase\fP * \fBaccept\fP ()"
.br
.ti -1c
.RI "virtual \fBKSocketAddress\fP \fBlocalAddress\fP () const "
.br
.ti -1c
.RI "virtual \fBKSocketAddress\fP \fBexternalAddress\fP () const "
.br
.in -1c
.SS "Protected Member Functions"

.in +1c
.ti -1c
.RI "virtual bool \fBsetSocketOptions\fP (int opts)"
.br
.ti -1c
.RI "void \fBcopyError\fP ()"
.br
.in -1c
.SH "Detailed Description"
.PP 
A server socket for accepting connections. 

This class provides functionality for creating a socket to listen for incoming connections and subsequently accept them.
.PP
To use this class, you must first set the parameters for the listening socket's address, then place it in listening mode.
.PP
A typical example would look like: 
.PP
.nf
   QString service = 'http';
   KServerSocket *ss = new KServerSocket(service);
   connect(ss, SIGNAL(readyAccept()), this, SLOT(slotReadyAccept()));
   connect(ss, SIGNAL(gotError(int)), this, SLOT(slotSocketError(int)));
   ss->listen();

.fi
.PP
.PP
In this case, this class will place the socket into listening mode on the service pointed to by \fCservice\fP and will emit the \fBreadyAccept\fP signal when a connection is ready for accepting. The called slot is responsible for calling \fBaccept\fP.
.PP
The location of the services file (where \fCservice\fP 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 \fCservice\fP 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.
.PP
For another example of usage, this below code attempts to make a connection on any port within a range: 
.PP
.nf
   KServerSocket *ss = new KServerSocket();
   ss->setFamily(KResolver::InetFamily);
   bool found = false;
   for( unsigned int port = firstport; port <= lastport; ++port) {
     ss->setAddress( QString::number( port ) );
     bool success = ss->listen();
     if( found = ( success && ss->error() == 
                              KSocketBase::NoError ) )
       break;
     ss->close();
   }
   if( !found ) {
     //Couldn't connect to any port.
   } else {
     connect(ss, SIGNAL(readyAccept()), this, SLOT(slotReadyAccept()));
     connect(ss, SIGNAL(gotError(int)), this, SLOT(slotSocketError(int)));
     ss->listen();
   }

.fi
.PP
 The called slot slotReadyAccept() is responsible for calling \fBaccept\fP.
.PP
\fBAuthor:\fP
.RS 4
Thiago Macieira <thiago.macieira@kdemail.net> 
.RE
.PP

.PP
Definition at line 97 of file kserversocket.h.
.SH "Constructor & Destructor Documentation"
.PP 
.SS "KServerSocket::KServerSocket (\fBQObject\fP * parent = \fC0L\fP, const char * name = \fC0L\fP)"
.PP
Default constructor. 
.PP
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.
.PP
\fBParameters:\fP
.RS 4
\fIparent\fP the parent \fBQObject\fP object 
.br
\fIname\fP the name of this object 
.RE
.PP

.PP
Definition at line 61 of file kserversocket.cpp.
.PP
References QObject::connect().
.SS "KServerSocket::KServerSocket (const \fBQString\fP & service, \fBQObject\fP * parent = \fC0L\fP, const char * name = \fC0L\fP)"
.PP
Construct this object specifying the service to listen on. 
.PP
If the binding address isn't changed by setAddress, this socket will bind to all interfaces and will listen on the port specified by \fCservice\fP. This is either a service name (e.g. 'www') or a port number (e.g. '80').
.PP
The location of the services file (where \fCservice\fP 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.
.PP
\fBParameters:\fP
.RS 4
\fIservice\fP the service name to listen on 
.br
\fIparent\fP the parent \fBQObject\fP object 
.br
\fIname\fP the name of this object 
.RE
.PP

.PP
Definition at line 68 of file kserversocket.cpp.
.PP
References QObject::connect().
.SS "KServerSocket::KServerSocket (const \fBQString\fP & node, const \fBQString\fP & service, \fBQObject\fP * parent = \fC0L\fP, const char * name = \fC0L\fP)"
.PP
Construct this object specifying the node and service names to listen on. 
.PP
If the binding address isn't changed by setAddress, this socket will bind to the interface specified by \fCnode\fP and the port specified by \fCservice\fP. This is either a service name (e.g. 'www') or a port number (e.g. '80').
.PP
The location of the services file (where \fCservice\fP 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.
.PP
\fBParameters:\fP
.RS 4
\fInode\fP the node to bind to 
.br
\fIservice\fP the service port to listen on 
.br
\fIparent\fP the parent \fBQObject\fP object 
.br
\fIname\fP the name of this object 
.RE
.PP

.PP
Definition at line 76 of file kserversocket.cpp.
.PP
References QObject::connect(), and setAddress().
.SS "KServerSocket::~\fBKServerSocket\fP ()"
.PP
Destructor. 
.PP
This will close the socket, if open.
.PP
Note, however, that accepted sockets do not get closed when this object closes. 
.PP
Definition at line 85 of file kserversocket.cpp.
.PP
References close().
.SH "Member Function Documentation"
.PP 
.SS "bool KServerSocket::setSocketOptions (int opts)\fC [protected, virtual]\fP"
.PP
Sets the socket options. 
.PP
Reimplemented from \fBKSocketBase\fP. 
.PP
Reimplemented from \fBKNetwork::KSocketBase\fP.
.PP
Definition at line 91 of file kserversocket.cpp.
.PP
References copyError(), KNetwork::KSocketBase::mutex(), KNetwork::KSocketDevice::setSocketOptions(), and KNetwork::KSocketBase::socketDevice().
.SS "\fBKResolver\fP & KServerSocket::resolver () const"
.PP
Returns the internal \fBKResolver\fP object used for looking up the host name and service. 
.PP
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. 
.PP
Definition at line 100 of file kserversocket.cpp.
.SS "const \fBKResolverResults\fP & KServerSocket::resolverResults () const"
.PP
Returns the internal list of resolved results for the binding address. 
.PP
Definition at line 105 of file kserversocket.cpp.
.SS "void KServerSocket::setResolutionEnabled (bool enable)"
.PP
Enables or disables name resolution. 
.PP
If this flag is set to true, the \fBbind\fP 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.
.PP
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 \fBKSocketAddress\fP.
.PP
\fBParameters:\fP
.RS 4
\fIenable\fP whether to enable 
.RE
.PP

.PP
Definition at line 110 of file kserversocket.cpp.
.SS "void KServerSocket::setFamily (int families)"
.PP
Sets the allowed families for the resolutions. 
.PP
\fBParameters:\fP
.RS 4
\fIfamilies\fP the families that we want/accept 
.RE
.PP
\fBSee also:\fP
.RS 4
\fBKResolver::SocketFamilies\fP for possible values 
.RE
.PP

.PP
Definition at line 118 of file kserversocket.cpp.
.SS "void KServerSocket::setAddress (const \fBQString\fP & service)"
.PP
Sets the address on which we will listen. 
.PP
The port to listen on is given by \fCservice\fP, and we will bind to all interfaces. To let the operating system choose a port, set the service to '0'. \fCservice\fP can either be a service name (e.g. 'www') or a port number (e.g. '80').
.PP
The location of the services file (where \fCservice\fP 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.
.PP
\fBParameters:\fP
.RS 4
\fIservice\fP the service name to listen on 
.RE
.PP

.PP
Definition at line 123 of file kserversocket.cpp.
.PP
References QIODevice::state().
.PP
Referenced by bind(), and KServerSocket().
.SS "void KServerSocket::setAddress (const \fBQString\fP & node, const \fBQString\fP & service)"
.PP
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
.PP
Sets the address on which we will listen. This will cause the socket to listen only on the interface given by \fCnode\fP and on the port given by \fCservice\fP. \fCservice\fP can either be a service name (e.g. 'www') or a port number (e.g. '80').
.PP
The location of the services file (where \fCservice\fP 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.
.PP
\fBParameters:\fP
.RS 4
\fInode\fP the node to bind to 
.br
\fIservice\fP the service port to listen on 
.RE
.PP

.PP
Definition at line 132 of file kserversocket.cpp.
.PP
References QIODevice::state().
.SS "void KServerSocket::setTimeout (int msecs)"
.PP
Sets the timeout for accepting. 
.PP
When you call \fBaccept\fP, it will wait at most \fCmsecs\fP milliseconds or return with an error (returning a NULL object).
.PP
\fBParameters:\fP
.RS 4
\fImsecs\fP the time in milliseconds to wait, 0 to wait forever 
.RE
.PP

.PP
Definition at line 141 of file kserversocket.cpp.
.SS "bool KServerSocket::lookup ()\fC [virtual]\fP"
.PP
Starts the lookup for peer and local hostnames as well as their services. 
.PP
If the blocking mode for this object is on, this function will wait for the lookup results to be available (by calling the \fBKResolver::wait\fP method on the resolver objects).
.PP
When the lookup is done, the signal \fBhostFound\fP will be emitted (only once, even if we're doing a double lookup). If the lookup failed (for any of the two lookups) the \fBgotError\fP signal will be emitted with the appropriate error condition (see \fBKSocketBase::SocketError\fP).
.PP
This function returns true on success and false on error. Note that this is not the lookup result! 
.PP
Definition at line 146 of file kserversocket.cpp.
.PP
References KNetwork::KSocketBase::blocking(), and KNetwork::KSocketBase::setError().
.PP
Referenced by bind().
.SS "bool KServerSocket::bind (const \fBQString\fP & node, const \fBQString\fP & service)\fC [virtual]\fP"
.PP
Binds this socket to the given nodename and service, or use the default ones if none are given. 
.PP
Upon successful binding, the \fBbound\fP signal will be emitted. If an error is found, the \fBgotError\fP signal will be emitted.
.PP
This function returns true on success.
.PP
\fBParameters:\fP
.RS 4
\fInode\fP the nodename 
.br
\fIservice\fP the service 
.RE
.PP

.PP
Definition at line 196 of file kserversocket.cpp.
.PP
References bind(), and setAddress().
.SS "bool KServerSocket::bind (const \fBQString\fP & service)\fC [virtual]\fP"
.PP
Binds the socket to the given service name. 
.PP
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
.PP
\fBParameters:\fP
.RS 4
\fIservice\fP the service 
.RE
.PP

.PP
Definition at line 202 of file kserversocket.cpp.
.PP
References bind(), and setAddress().
.SS "bool KServerSocket::bind ()\fC [virtual]\fP"
.PP
Binds the socket to the addresses previously set with \fBsetAddress\fP. 
.PP
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 
.PP
Definition at line 208 of file kserversocket.cpp.
.PP
References KNetwork::KSocketBase::blocking(), and lookup().
.PP
Referenced by bind(), and listen().
.SS "bool KServerSocket::bind (const \fBKResolverEntry\fP & address)\fC [virtual]\fP"
.PP
Connect this socket to this specific address. 
.PP
Reimplemented from \fBKSocketBase\fP.
.PP
Unlike \fBbind(const QString&, const QString&)\fP above, this function really does bind the socket. No lookup is performed. The \fBbound\fP signal will be emitted. 
.PP
Implements \fBKNetwork::KPassiveSocketBase\fP.
.PP
Definition at line 182 of file kserversocket.cpp.
.PP
References bind(), bound(), copyError(), KNetwork::KSocketBase::setError(), KNetwork::KSocketBase::socketDevice(), and QIODevice::state().
.SS "bool KServerSocket::listen (int backlog = \fC5\fP)\fC [virtual]\fP"
.PP
Puts this socket into listening mode. 
.PP
Reimplemented from \fBKPassiveSocketBase\fP.
.PP
Placing a socket into listening mode means it will be able to receive incoming connections through the \fBaccept\fP method.
.PP
If you do not call this method but call \fBaccept\fP directly, the socket will be placed into listening mode automatically.
.PP
\fBParameters:\fP
.RS 4
\fIbacklog\fP the number of connection the system is to queue without \fBaccept\fP being called 
.RE
.PP
\fBReturns:\fP
.RS 4
true if the socket is now in listening mode. 
.RE
.PP

.PP
Implements \fBKNetwork::KPassiveSocketBase\fP.
.PP
Definition at line 232 of file kserversocket.cpp.
.PP
References bind().
.PP
Referenced by accept().
.SS "void KServerSocket::close ()\fC [virtual]\fP"
.PP
Closes this socket. 
.PP
Implements \fBKNetwork::KPassiveSocketBase\fP.
.PP
Definition at line 269 of file kserversocket.cpp.
.PP
References KNetwork::KSocketDevice::close(), closed(), KNetwork::KSocketBase::socketDevice(), and QIODevice::state().
.PP
Referenced by ~KServerSocket().
.SS "void KServerSocket::setAcceptBuffered (bool enable)"
.PP
Toggles whether the accepted socket will be buffered or not. 
.PP
That is, the \fBaccept\fP function will always return a \fBKStreamSocket\fP object or descended from it. If buffering is enabled, the class to be returned will be \fBKBufferedSocket\fP.
.PP
By default, this flag is set to true.
.PP
\fBParameters:\fP
.RS 4
\fIenable\fP whether to set the accepted socket to buffered mode 
.RE
.PP

.PP
Definition at line 278 of file kserversocket.cpp.
.SS "\fBKActiveSocketBase\fP * KServerSocket::accept ()\fC [virtual]\fP"
.PP
Accepts one incoming connection and return the associated, open socket. 
.PP
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 \fBKStreamSocket\fP.
.PP
\fBNote:\fP
.RS 4
This function should return a \fBKStreamSocket\fP 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 \fBKStreamSocket\fP or derived from it.
.RE
.PP
\fBSee also:\fP
.RS 4
\fBKBufferedSocket\fP 
.PP
\fBsetAcceptBuffered\fP 
.RE
.PP

.PP
Implements \fBKNetwork::KPassiveSocketBase\fP.
.PP
Definition at line 283 of file kserversocket.cpp.
.PP
References KNetwork::KSocketDevice::accept(), KNetwork::KSocketBase::blocking(), copyError(), listen(), KNetwork::KSocketBase::setError(), KNetwork::KSocketBase::setSocketDevice(), KNetwork::KClientSocketBase::setState(), and KNetwork::KSocketBase::socketDevice().
.SS "\fBKSocketAddress\fP KServerSocket::localAddress () const\fC [virtual]\fP"
.PP
Returns this socket's local address. 
.PP
Implements \fBKNetwork::KPassiveSocketBase\fP.
.PP
Definition at line 337 of file kserversocket.cpp.
.PP
References KNetwork::KSocketDevice::localAddress(), and KNetwork::KSocketBase::socketDevice().
.SS "\fBKSocketAddress\fP KServerSocket::externalAddress () const\fC [virtual]\fP"
.PP
Returns this socket's externally-visible address if know. 
.PP
Implements \fBKNetwork::KPassiveSocketBase\fP.
.PP
Definition at line 342 of file kserversocket.cpp.
.PP
References KNetwork::KSocketDevice::externalAddress(), and KNetwork::KSocketBase::socketDevice().
.SS "void KNetwork::KServerSocket::gotError (int code)\fC [signal]\fP"
.PP
This signal is emitted when this object finds an error. 
.PP
The \fCcode\fP parameter contains the error code that can also be found by calling \fBerror\fP. 
.SS "void KNetwork::KServerSocket::hostFound ()\fC [signal]\fP"
.PP
This signal is emitted when the lookup is successfully completed. 
.PP
.SS "void KNetwork::KServerSocket::bound (const \fBKResolverEntry\fP & local)\fC [signal]\fP"
.PP
This signal is emitted when the socket successfully binds to an address. 
.PP
\fBParameters:\fP
.RS 4
\fIlocal\fP the local address we bound to 
.RE
.PP

.PP
Referenced by bind().
.SS "void KNetwork::KServerSocket::closed ()\fC [signal]\fP"
.PP
This signal is emitted when the socket completes the closing/shut down process. 
.PP
Referenced by close().
.SS "void KNetwork::KServerSocket::readyAccept ()\fC [signal]\fP"
.PP
This signal is emitted whenever the socket is ready for accepting -- i.e., there is at least one connection waiting to be accepted. 
.PP
.SS "void KServerSocket::copyError ()\fC [protected]\fP"
.PP
Convenience function to set this object's error code to match that of the socket device. 
.PP
Definition at line 370 of file kserversocket.cpp.
.PP
References KNetwork::KSocketBase::error(), KNetwork::KSocketBase::setError(), and KNetwork::KSocketBase::socketDevice().
.PP
Referenced by accept(), bind(), and setSocketOptions().

.SH "Author"
.PP 
Generated automatically by Doxygen for kdecore Library from the source code.
