Index: libbtcore/torrent/server.cpp =================================================================== --- libbtcore/torrent/server.cpp (revision 777233) +++ libbtcore/torrent/server.cpp (working copy) @@ -57,7 +57,7 @@ bool Server::isOK() const { - return sock && sock->ok(); + return sock && sock->ok() && (sock->state() == net::Socket::BOUND); } void Server::changePort(Uint16 p) @@ -71,7 +71,7 @@ port = p; delete sock; sock = 0; - delete sn; + delete sn; sn = 0; QString iface = NetworkInterface(); Index: ktorrent/core.cpp =================================================================== --- ktorrent/core.cpp (revision 777233) +++ ktorrent/core.cpp (working copy) @@ -92,31 +92,9 @@ port = 6881; Settings::setPort(6881); } - Uint16 i = 0; - do - { - Globals::instance().initServer(port + i); - i++; - } - while (!Globals::instance().getServer().isOK() && i < 10); + // XXX: this can fail silently, see changePort (called through applySettings) for the error handling code + Globals::instance().initServer(port); - if (Globals::instance().getServer().isOK()) - { - if (port != port + i - 1) - gui->infoMsg(i18n("Specified port (%1) is unavailable or in" - " use by another application. KTorrent is now using port %2.", - port,QString::number(port + i - 1))); - - Out(SYS_GEN|LOG_NOTICE) << "Bound to port " << (port + i - 1) << endl; - } - else - { - gui->errorMsg(i18n("KTorrent is unable to accept connections because the ports %1 to %2 are " - "already in use by another program.",port,QString::number(port + i - 1))); - Out(SYS_GEN|LOG_IMPORTANT) << "Cannot find free port" << endl; - } - - pman = new kt::PluginManager(this,gui); gman = new kt::GroupManager(); applySettings(true); @@ -781,7 +759,30 @@ { if (qman->count() == 0) { - Globals::instance().getServer().changePort(port); + Uint16 i = 0; + do + { + Globals::instance().getServer().changePort(port + i); + i++; + } + while (!Globals::instance().getServer().isOK() && i < 10); + + if (Globals::instance().getServer().isOK()) + { + if (port != port + i - 1) + gui->infoMsg(i18n("Specified port (%1) is unavailable or in" + " use by another application. KTorrent is now using port %2.", + port,QString::number(port + i - 1))); + + Out(SYS_GEN|LOG_NOTICE) << "Bound to port " << (port + i - 1) << endl; + } + else + { + gui->errorMsg(i18n("KTorrent is unable to accept connections because the ports %1 to %2 are " + "already in use by another program.",port,QString::number(port + i - 1))); + Out(SYS_GEN|LOG_IMPORTANT) << "Cannot find free port" << endl; + } + // XXX: only return true here when it was successful? (if so only when we were able to bind to the port we want or as long as we can bind?) return true; } else