Modify bindAndListen to allow suppression of warnings when bind failures are expected as part of normal operation

This relates to Bug 1988
pull/16/head
Timothy Pearson 10 years ago
parent 319f69a280
commit 379f344bb3

@ -20,6 +20,8 @@
* Boston, MA 02110-1301, USA. * Boston, MA 02110-1301, USA.
**/ **/
#define KSOCK_INTERNAL_C_COMPILATION 1
#include <config.h> #include <config.h>
#include <sys/types.h> #include <sys/types.h>
@ -312,7 +314,7 @@ bool TDEServerSocket::init( const char *_path )
d->ks = ks; d->ks = ks;
if (d->bind) if (d->bind)
return bindAndListen(); return bindAndListen(false);
return true; return true;
} }
@ -326,11 +328,11 @@ bool TDEServerSocket::init( unsigned short int _port )
d->ks = ks; d->ks = ks;
if (d->bind) if (d->bind)
return bindAndListen(); return bindAndListen(false);
return true; return true;
} }
bool TDEServerSocket::bindAndListen() bool TDEServerSocket::bindAndListen(bool suppressFailureMessages)
{ {
if (d == NULL || d->ks == NULL) if (d == NULL || d->ks == NULL)
return false; return false;
@ -339,7 +341,10 @@ bool TDEServerSocket::bindAndListen()
int ret = d->ks->listen( SOMAXCONN ); int ret = d->ks->listen( SOMAXCONN );
if (ret < 0) if (ret < 0)
{ {
kdWarning(170) << "Error listening on socket: " << ret << "\n"; if (!suppressFailureMessages)
{
kdWarning(170) << "Error listening on socket for port " << d->ks->port() << ": " << ret << "\n";
}
delete d->ks; delete d->ks;
d->ks = NULL; d->ks = NULL;
sock = -1; sock = -1;
@ -432,4 +437,10 @@ TDEServerSocket::~TDEServerSocket()
// ::close( sock ); // ::close( sock );
} }
// DEPRECATED
bool TDEServerSocket::bindAndListen()
{
return bindAndListen(false);
}
#include "ksock.moc" #include "ksock.moc"

@ -282,9 +282,12 @@ public:
* Binds the socket and start listening. This should only be called * Binds the socket and start listening. This should only be called
* once when the constructor was called with _bind false. * once when the constructor was called with _bind false.
* On error the socket will be closed. * On error the socket will be closed.
* @param suppressFailureMessages suppress warning messages generated if the socket cannot be opened.
* @return true on success. false on error. * @return true on success. false on error.
* @warning If suppressFailureMessages is TRUE future debugging may be made more difficult. Only set it
* if your application expects to bind to unavailable ports, e.g. while scanning for open ports in a range.
*/ */
bool bindAndListen(); bool bindAndListen(bool suppressFailureMessages = false);
/** /**
* Returns the file descriptor associated with the socket. * Returns the file descriptor associated with the socket.
@ -339,6 +342,11 @@ protected:
int sock; int sock;
private: private:
// DEPRECATED
#ifdef KSOCK_INTERNAL_C_COMPILATION
KDE_EXPORT bool bindAndListen();
#endif // KSOCK_INTERNAL_C_COMPILATION
TDEServerSocket(const TDEServerSocket&); TDEServerSocket(const TDEServerSocket&);
TDEServerSocket& operator=(const TDEServerSocket&); TDEServerSocket& operator=(const TDEServerSocket&);

Loading…
Cancel
Save