From 4efe081084f37c92a5c9d9ad53f70241c5450596 Mon Sep 17 00:00:00 2001 From: Stanislas Michalak Date: Sun, 19 Apr 2020 23:51:33 +0200 Subject: [PATCH 1/2] Allow client to bind on a random port --- src/socket.cpp | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/socket.cpp b/src/socket.cpp index f2a60c02..c33d47a3 100755 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -54,23 +54,35 @@ void CSocket::Init ( const quint16 iPortNumber ) if ( bIsClient ) { - // Per definition use the port number plus ten 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 = 10; // start value: port nubmer plus ten - bSuccess = false; // initialization for while loop - - while ( !bSuccess && - ( iClientPortIncrement <= NUM_SOCKET_PORTS_TO_TRY ) ) + if (iPortNumber == 0) { - UdpSocketInAddr.sin_port = htons ( iPortNumber + iClientPortIncrement ); + // If port number is 0, bind the client to a random available port. + UdpSocketInAddr.sin_port = htons ( iPortNumber ); bSuccess = ( ::bind ( UdpSocket , - (sockaddr*) &UdpSocketInAddr, - sizeof ( sockaddr_in ) ) == 0 ); + (sockaddr*) &UdpSocketInAddr, + sizeof ( sockaddr_in ) ) == 0 ); + } + else + { + // Per definition use the port number plus ten 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 = 10; // start value: port nubmer plus ten + bSuccess = false; // initialization for while loop - iClientPortIncrement++; + while ( !bSuccess && + ( iClientPortIncrement <= NUM_SOCKET_PORTS_TO_TRY ) ) + { + UdpSocketInAddr.sin_port = htons ( iPortNumber + iClientPortIncrement ); + + bSuccess = ( ::bind ( UdpSocket , + (sockaddr*) &UdpSocketInAddr, + sizeof ( sockaddr_in ) ) == 0 ); + + iClientPortIncrement++; + } } } else From f0e73cf81b375ed4c03b2b97cb0767fb853ca7b3 Mon Sep 17 00:00:00 2001 From: Stanislas Michalak Date: Mon, 20 Apr 2020 09:20:00 +0200 Subject: [PATCH 2/2] Explicitly set htons to 0 Also fix a typo. --- src/socket.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/socket.cpp b/src/socket.cpp index c33d47a3..77f1c826 100755 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -57,7 +57,7 @@ void CSocket::Init ( const quint16 iPortNumber ) if (iPortNumber == 0) { // If port number is 0, bind the client to a random available port. - UdpSocketInAddr.sin_port = htons ( iPortNumber ); + UdpSocketInAddr.sin_port = htons ( 0 ); bSuccess = ( ::bind ( UdpSocket , (sockaddr*) &UdpSocketInAddr, @@ -88,7 +88,7 @@ void CSocket::Init ( const quint16 iPortNumber ) else { // for the server, only try the given port number and do not try out - // other port numbers to bind since it is imporatant that the server + // other port numbers to bind since it is important that the server // gets the desired port number UdpSocketInAddr.sin_port = htons ( iPortNumber );