Skip to content

Commit

Permalink
Explain the logic behind port reservation more in detail
Browse files Browse the repository at this point in the history
  • Loading branch information
pNre committed Aug 9, 2024
1 parent cf28128 commit b2c7835
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Sources/SBTUITestTunnelClient/SBTUITestTunnelClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,12 @@ - (int)findOpenPort
continue;
}

// Put the port in the TIME_WAIT state, to temporarily reserve it
// Attempt to reserve the port by putting it in TIME_WAIT state. During this time,
// the system prevents other applications from binding to the same port,
// to prevent packets meant for the recently closed connection from being
// misdirected to the new application. Since SBTWebServer is utilizing SO_REUSEADDR on the
// server socket, we can bind to the port even though it's in TIME_WAIT state,
// effectively reserving it for our own use until we close the server socket.
if (listen(server_sock, 1)) {
close(server_sock);
return -4;
Expand Down

0 comments on commit b2c7835

Please sign in to comment.