give more flexibility with the client port numbers
This commit is contained in:
parent
24f707f2e7
commit
1d4a7df6b5
3 changed files with 23 additions and 7 deletions
|
@ -259,7 +259,7 @@ int main ( int argc, char** argv )
|
||||||
if ( !bIsClient && !strCentralServer.isEmpty() )
|
if ( !bIsClient && !strCentralServer.isEmpty() )
|
||||||
{
|
{
|
||||||
bIsCentralServer =
|
bIsCentralServer =
|
||||||
( !strCentralServer.compare ( "localhost" ) ||
|
( !strCentralServer.toLower().compare ( "localhost" ) ||
|
||||||
!strCentralServer.compare ( "127.0.0.1" ) );
|
!strCentralServer.compare ( "127.0.0.1" ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,14 +32,25 @@ void CSocket::Init ( const quint16 iPortNumber )
|
||||||
// allocate memory for network receive and send buffer in samples
|
// allocate memory for network receive and send buffer in samples
|
||||||
vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF );
|
vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF );
|
||||||
|
|
||||||
// initialize the listening socket, per definition use the port number plus
|
// initialize the listening socket
|
||||||
// one for the client to make it possible to run server and client on the
|
|
||||||
// same computer
|
|
||||||
bool bSuccess;
|
bool bSuccess;
|
||||||
if ( bIsClient )
|
if ( bIsClient )
|
||||||
|
{
|
||||||
|
// 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 (
|
bSuccess = SocketDevice.bind (
|
||||||
QHostAddress( QHostAddress::Any ), iPortNumber + 1 );
|
QHostAddress( QHostAddress::Any ),
|
||||||
|
iPortNumber + iClientPortIncrement );
|
||||||
|
|
||||||
|
iClientPortIncrement++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -42,6 +42,11 @@
|
||||||
class CServer; // forward declaration of CServer
|
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 ********************************************************************/
|
/* Classes ********************************************************************/
|
||||||
class CSocket : public QObject
|
class CSocket : public QObject
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue