-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
119 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
#ifndef SIMPLE_SOCKET_AVAILABLEPORTQUERY_HPP | ||
#define SIMPLE_SOCKET_AVAILABLEPORTQUERY_HPP | ||
|
||
|
||
int getAvailablePort(int startPort, int endPort); | ||
|
||
#endif//SIMPLE_SOCKET_AVAILABLEPORTQUERY_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
#include "AvailablePortQuery.hpp" | ||
|
||
#include "SocketIncludes.hpp" | ||
|
||
|
||
int getAvailablePort(int startPort, int endPort) { | ||
int sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
if (sockfd == SOCKET_ERROR) { | ||
return -1; | ||
} | ||
|
||
const int optval = 1; | ||
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&optval), sizeof(optval)); | ||
|
||
sockaddr_in serv_addr{}; | ||
serv_addr.sin_family = AF_INET; | ||
serv_addr.sin_addr.s_addr = INADDR_ANY; | ||
|
||
for (int port = startPort; port <= endPort; ++port) { | ||
serv_addr.sin_port = htons(port); | ||
if (bind(sockfd, reinterpret_cast<sockaddr*>(&serv_addr), sizeof(serv_addr)) != SOCKET_ERROR) { | ||
closeSocket(sockfd); | ||
|
||
return port; | ||
} | ||
} | ||
|
||
closeSocket(sockfd); | ||
|
||
return -1; // No available port found | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
#ifndef SIMPLE_SOCKET_SOCKETINCLUDES_HPP | ||
#define SIMPLE_SOCKET_SOCKETINCLUDES_HPP | ||
|
||
|
||
#ifdef _WIN32 | ||
#include <winsock2.h> | ||
#include <ws2tcpip.h> | ||
#else | ||
#include <arpa/inet.h> | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
using SOCKET = int; | ||
#define INVALID_SOCKET (SOCKET)(~0) | ||
#define SOCKET_ERROR (-1) | ||
#endif | ||
|
||
#include <system_error> | ||
|
||
|
||
inline void throwError(const std::string& msg) { | ||
|
||
#ifdef _WIN32 | ||
throw std::system_error(WSAGetLastError(), std::system_category(), msg); | ||
#else | ||
throw std::system_error(errno, std::generic_category(), msg); | ||
#endif | ||
} | ||
|
||
inline void closeSocket(SOCKET socket) { | ||
|
||
#ifdef WIN32 | ||
closesocket(socket); | ||
#else | ||
close(socket); | ||
#endif | ||
} | ||
|
||
|
||
#endif//SIMPLE_SOCKET_SOCKETINCLUDES_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters