give more flexibility with the client port numbers

This commit is contained in:
Volker Fischer 2011-04-07 18:26:54 +00:00
parent 24f707f2e7
commit 1d4a7df6b5
3 changed files with 23 additions and 7 deletions

View File

@ -259,7 +259,7 @@ int main ( int argc, char** argv )
if ( !bIsClient && !strCentralServer.isEmpty() )
{
bIsCentralServer =
( !strCentralServer.compare ( "localhost" ) ||
( !strCentralServer.toLower().compare ( "localhost" ) ||
!strCentralServer.compare ( "127.0.0.1" ) );
}

View File

@ -32,14 +32,25 @@ void CSocket::Init ( const quint16 iPortNumber )
// allocate memory for network receive and send buffer in samples
vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF );
// initialize the listening socket, per definition use the port number plus
// one for the client to make it possible to run server and client on the
// same computer
// initialize the listening socket
bool bSuccess;
if ( bIsClient )
{
bSuccess = SocketDevice.bind (
QHostAddress( QHostAddress::Any ), iPortNumber + 1 );
// Per definition use the port number plus one for the client to make
// it possible to run server and client on the same computer. If the
// port is not available, try "NUM_SOCKET_PORTS_TO_TRY" times with
// incremented port numbers
quint16 iClientPortIncrement = 1; // start value: port nubmer plus one
bSuccess = false; // initialization for while loop
while ( !bSuccess &&
( iClientPortIncrement <= NUM_SOCKET_PORTS_TO_TRY ) )
{
bSuccess = SocketDevice.bind (
QHostAddress( QHostAddress::Any ),
iPortNumber + iClientPortIncrement );
iClientPortIncrement++;
}
}
else
{

View File

@ -42,6 +42,11 @@
class CServer; // forward declaration of CServer
/* Definitions ****************************************************************/
// number of ports we try to bind until we give up
#define NUM_SOCKET_PORTS_TO_TRY 50
/* Classes ********************************************************************/
class CSocket : public QObject
{
@ -59,7 +64,7 @@ public:
: pServer ( pNServP ), bIsClient ( false ) { Init ( iPortNumber ); }
void SendPacket ( const CVector<uint8_t>& vecbySendBuf,
const CHostAddress& HostAddr );
const CHostAddress& HostAddr );
protected:
void Init ( const quint16 iPortNumber = LLCON_DEFAULT_PORT_NUMBER );