Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,19 @@ void CSocket::Init ( const quint16 iPortNumber )
{
// if the port is not available, try "NUM_SOCKET_PORTS_TO_TRY" times
// with incremented port numbers

// Randomize the start by 5 ports, in case a faulty router gets
// stuck and confused by a particular port (like the starting port).
// Might work around frustrating "cannot connect" problems.

quint16 startingPortNumber = iPortNumber + rand() % 5;

quint16 iClientPortIncrement = 0;
bSuccess = false; // initialization for while loop

while ( !bSuccess && ( iClientPortIncrement <= NUM_SOCKET_PORTS_TO_TRY ) )
{
UdpSocketInAddr.sin_port = htons ( iPortNumber + iClientPortIncrement );
UdpSocketInAddr.sin_port = htons ( startingPortNumber + iClientPortIncrement );

bSuccess = ( ::bind ( UdpSocket ,
(sockaddr*) &UdpSocketInAddr,
Expand Down