Secure and non-secure version of Socket classes. You need OpenSSL for secure version. If you want to use these classes, do not forget to include SocketException and SecureSocketException which inherited from BaseException in the organization directory.
try {
auto clientSocket = std::make_unique<sdk::network::Socket>(portNumber);
clientSocket->setIpAddress("127.0.0.1");
SocketOption<Socket> socketOpt{ *clientSocket };
socketOpt.setBlockingMode(1); //set non-blocking mode is active
clientSocket->connect(); //connect to the server
auto socketDesc = clientSocket->createSocketDescriptor(clientSocket->getSocketId());
std::string response;
socketDesc->write("Some important messages from client!");
socketDesc->read(response);
std::cout << "response from the server: " << response << "\n";
}
catch (const sdk::general::SocketException& ex)
{
std::cout << "Err code: " << ex.getErrorCode() << ", Err Msg: " << ex.getErrorMsg() << "\n";
}
try {
static const char* certFile = "C:\\Program Files\\OpenSSL\\bin\\mycert.pem";
static const char* keyFile = "C:\\Program Files\\OpenSSL\\bin\\privateKey.key";
auto clientSSLSocket = std::make_unique<sdk::network::SSLSocket>(portNumber, connection_method::client);
clientSSLSocket->setIpAddress("127.0.0.1");
SocketOption<SSLSocket> socketOpt{ *clientSSLSocket };
socketOpt.setBlockingMode(1); //set non-blocking mode is active
clientSSLSocket->connect(); //connect to the server
clientSSLSocket->loadCertificateFile(certFile);
clientSSLSocket->loadPrivateKeyFile(keyFile);
auto socketSSLDesc = clientSSLSocket->createSocketDescriptor(clientSSLSocket->getSocketId());
socketSSLDesc->connect();
std::string response;
socketSSLDesc->write("Some important messages from client!");
socketSSLDesc->read(response);
std::cout << "response from the server: " << response << "\n";
}
catch (const sdk::general::SecureSocketException& ex)
{
std::cout << "Err code: " << ex.getErrorCode() << ", Err Msg: " << ex.getErrorMsg() << "\n";
}
Before compile OpenSSL you need Strawberry Perl that you can download and install from: https://strawberryperl.com/. You also need nasm assembler which can be downloaded and installed from: https://www.nasm.us/
- Download the lastest (v.3.x.x) source files from: https://www.openssl.org/source/.
- Extract the zipped file to the local disk(C) (For example C:\openssl)
- Run the Native Tools Command Prompt for VS (x86 or x64 which arch will be compiled) as Administrator.
- Type the following commands step by step:
cd C:/openssl
perl configure VC-WIN32 no-shared (for x86)
perl configure VC-WIN64A no-shared (for x64)
nmake
nmake install
- It will take a while the last two steps. So you can take a cup of coffee or tea and relax :)
If you have any questions, please do not hesitate to ask me!