Skip to content

Commit

Permalink
Server port fix
Browse files Browse the repository at this point in the history
  • Loading branch information
theokyr authored Jul 7, 2024
2 parents 4bf642b + 5b870db commit 4e45434
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
17 changes: 15 additions & 2 deletions CS2RemoteConsole-server/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "utils.h"
#include <iostream>
#include <csignal>
#include <cstdlib>
#include <string>

std::atomic<bool> applicationRunning(true);

Expand All @@ -11,11 +13,22 @@ void signalHandler(int signum)
applicationRunning = false;
}

int main()
int main(int argc, char* argv[])
{
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);

uint16_t port = 42069; // Default port

for (int i = 1; i < argc; i++)
{
std::string arg = argv[i];
if ((arg == "-p" || arg == "--port") && i + 1 < argc)
{
port = static_cast<uint16_t>(std::atoi(argv[++i]));
}
}

#ifdef _WIN32
if (!initializeWinsock())
{
Expand All @@ -24,7 +37,7 @@ int main()
}
#endif

Server server(42069, applicationRunning);
Server server(port, applicationRunning);
if (!server.start())
{
return 1;
Expand Down
67 changes: 50 additions & 17 deletions CS2RemoteConsole-server/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <sstream>
#include <algorithm>

Server::Server(uint16_t port, std::atomic<bool>& running)
: m_port(port), m_listenSocket(INVALID_SOCKET), m_running(running)
Server::Server(uint16_t initialPort, std::atomic<bool>& running)
: m_initialPort(initialPort), m_port(initialPort), m_listenSocket(INVALID_SOCKET), m_running(running)
{
}

Expand All @@ -16,23 +16,52 @@ Server::~Server()

bool Server::start()
{
m_listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_listenSocket == INVALID_SOCKET)
const int MAX_PORT = 65535; // Maximum valid port number

while (m_port <= MAX_PORT)
{
std::cerr << "Error creating socket: " << SOCKET_ERROR_CODE << std::endl;
return false;
}
std::cout << "Attempting to bind to port " << m_port << "..." << std::endl;

sockaddr_in serverAddr{};
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(m_port);
m_listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_listenSocket == INVALID_SOCKET)
{
std::cerr << "Error creating socket: " << SOCKET_ERROR_CODE << std::endl;
return false;
}

if (bind(m_listenSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
std::cerr << "Bind failed: " << SOCKET_ERROR_CODE << std::endl;
cleanupSockets();
return false;
int opt = 1;
if (setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt)) < 0)
{
std::cerr << "setsockopt(SO_REUSEADDR) failed: " << SOCKET_ERROR_CODE << std::endl;
cleanupSockets();
return false;
}

sockaddr_in serverAddr{};
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(m_port);

if (bind(m_listenSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
std::cerr << "Bind failed on port " << m_port << ": " << SOCKET_ERROR_CODE << std::endl;

cleanupSockets();

// Try the next port
m_port++;

if (m_port > MAX_PORT)
{
std::cerr << "Exhausted all available ports. Failed to start server." << std::endl;
return false;
}

continue;
}

// If we reach here, bind was successful
break;
}

if (listen(m_listenSocket, SOMAXCONN) == SOCKET_ERROR)
Expand All @@ -44,7 +73,11 @@ bool Server::start()

setNonBlocking(m_listenSocket);

std::cout << "Server is listening on port " << m_port << "..." << std::endl;
std::cout << "Server is listening on port " << m_port << std::endl;
if (m_port != m_initialPort)
{
std::cout << "Note: The server is using a different port than initially specified." << std::endl;
}
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion CS2RemoteConsole-server/src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
class Server
{
public:
Server(uint16_t port, std::atomic<bool>& running);
Server(uint16_t initialPort, std::atomic<bool>& running);
~Server();

bool start();
void run();
uint16_t getCurrentPort() const { return m_port; }

private:
uint16_t m_initialPort;
uint16_t m_port;
SOCKET m_listenSocket;
std::vector<ClientInfo> m_clients;
Expand Down

0 comments on commit 4e45434

Please sign in to comment.