Apr 23, 2024 07:08 AM
(This post was last modified: Apr 23, 2024 01:58 PM by nucleo_type09.)
C++ Memory-Safe Multi-Threaded Server Socket Connection Program, can accept multiple connections at the same time using a detached thread for each connection operation, provides memory safety and concurrency control using locking mechanism, uses an internal built in memory load balancing mechanism, the library is very easy to use, just create an instance and call Connect on it, and the server is up and running, you don't need to handle extra configurations, manageable configurations are: [ip-address, port number, IDLE timeout, auto shutdown mechanism, max n of peers, etc...], memory cleanup is already handled by the destructor.
Compiled using gcc compiler on Linux System using c++20, using additional memory security flags such as -fsanitizer-address for catching memory leaks, the full code and library documentation is on github, you can find it here: https://github.com/nucleoType98/Unix_Server_Socket.git
------------------------------------------------------------------------------------------------------------------------------------------------
Simple Connection
With More Configurations
Compiled using gcc compiler on Linux System using c++20, using additional memory security flags such as -fsanitizer-address for catching memory leaks, the full code and library documentation is on github, you can find it here: https://github.com/nucleoType98/Unix_Server_Socket.git
------------------------------------------------------------------------------------------------------------------------------------------------
Simple Connection
Code
#include "server.cpp"
using namespace ServerSocket;
int main()
{
Socket *SockChannel = new Socket();
SockChannel->Connect();
// server is up and running....
delete SockChannel;
return 0;
}With More Configurations
Code
#include "server.cpp"
using namespace ServerSocket;
int main(int argc, char **argv)
{
// create new Socket Instance, this can also be non-pointer
Socket *SockChannel = new Socket();
// @Optional: set verbosity to true, default is true
SockChannel->set_verbosity_mode(true);
// @Optional: set auto shutdown mode, after idle timeout expires it will self-shutdown, you need to set an idle timeout value for this to have
effect, by default it does NOT automatically shutdown
SockChannel->use_auto_shutdown();
// @Optional: set server max idle time in seconds before auto shutdown for inactivity
SockChannel->set_max_server_idle_time(10);
// @Optional: set server ip address, defaults to localhost AKA 127.0.0.1
SockChannel->set_ip_address("127.0.0.1");
// @Optional: default port is 3300, most systems use this port range for networking operations anyway
SockChannel->set_port(3300);
// @Optional: set the maximum number of peer connections before rejecting new ones, defaults to 100
SockChannel->accept_max_peers(50);
// @Optional: use resource memory balancer to prevent system crash
SockChannel->use_memory_balancer();
// @Optional: reserve x megabytes amount of memory to always be available for other system processes, is good practice to keep this
value not below 500
SockChannel->reserve_memory(900);
// @Required: connect socket server and wait for connections
SockChannel->Connect();
delete SockChannel;
return 0;
}

