Writing a Client Program That Uses RWSecureSocket
Example 1 is a client program that creates a secure socket and sends a message.
This example uses utility functions provided in args.h.
Example 1 – Client program that uses RWSecureSocket
// File: examples\secsock\manual\RWSecureSocketSimpleClient.cpp
 
#include <rw/secsock/RWSecureSocket.h>
#include <rw/secsock/RWSecureSocketContext.h>
#include <rw/secsock/RWSecureSocketPackageInit.h>
 
#include <rw/network/RWInetAddr.h>
#include <rw/network/RWWinSockInfo.h>
 
#include <iostream>
 
#include "args.h"
#include "secsockexampledefs.h"
 
using std::cout;
using std::cerr;
using std::endl;
 
int main(int argc, char **argv)
{
try {
RWWinSockInfo info; //1
RWSecureSocketPackageInit secsockInit; //2
#if defined(RW_SECSOCK_RNG_NEEDS_SEEDING)
RWSecureSocketPackageInit::seedRNGFromFile(SEED_DATA_FILE); //3
#endif
 
int port = parseClientCommandLine(argc, argv);
 
RWSecureSocketContext context; //4
context.prepareToAuthenticate(TRUSTED_CERTS_FILE); //5
 
RWInetAddr addr(port, “localhost”) //6
std::cout << "Connecting to " << addr.id() << std::endl;
RWSecureSocket sock(context); //7
 
sock.connect(addr); //8
sock.sendAtLeast("Hello World!"); //9
sock.close(); //10
}
catch(const RWInternalErr& ie) {
cerr << ie.why() << endl;
return 1;
} catch(const RWExternalErr& ee) {
cerr << ee.why() << endl;
return 1;
}
 
return 0; //11
}
//1 Constructs an RWWinSockInfo instance. Initializes the Winsock library under Windows, but has no effect under Unix.
//2 Initializes the Secure Sockets package and the underlying cryptographic library.
//3 Seeds the random number generator, if it is necessary on your platform. Uses the data stored in SEED_DATA_FILE. This file can be any file on your system. See Seeding the Random Number Generator for more information.
//4 Constructs a context object that holds the default parameters for all secure sockets and portals that you create.
//5 Gets the path and file name of the trusted certificates file. In this example, TRUSTED_CERTS_FILE, is located in the same directory as the executable.
The client side of an SSL/TLS connection needs a set of trusted certificates to use when verifying a server. This set of trusted certificates should include the certificate of the server and the certificate of every certificate authority that signed your server’s certificate, including the root certificate authority. Every certificate that your client trusts should be placed in a single file. For more information, see Obtaining Certificates.
//6 Creates an address object that refers to the specified port on “localhost” (the local machine).
//7 Creates a secure socket from the context
//8 Connects the socket to the specified port on “localhost”.
//9 Sends a message over the socket.
//10 Closes the socket.
//11 Executes destructors for RWWinSockInfo and RWSecureSocketPackageInit to clean up resources allocated by the underlying Winsock and cryptographic libraries.