| 44 | } // namespace |
| 45 | |
| 46 | int CtrlUtil::FindAvailablePort() const { |
| 47 | int sock = socket(AF_INET, SOCK_STREAM, 0); |
| 48 | |
| 49 | for (uint16_t port = 10000; port < GetMaxVal<uint16_t>(); ++port) { |
| 50 | sockaddr_in sa = GetSockAddr("0.0.0.0", port); |
| 51 | int bind_result = bind(sock, reinterpret_cast<sockaddr*>(&sa), sizeof(sa)); |
| 52 | if (bind_result == 0) { |
| 53 | shutdown(sock, SHUT_RDWR); |
| 54 | close(sock); |
| 55 | return port; |
| 56 | } |
| 57 | } |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | #else |
| 62 |