Client Example
Comments are included only for the lines that differ from the previous examples.
This example uses utility functions provided in args.h.
Example 7 – Writing a client program that performs client and server authentication
// File:
// examples\secsock\manual\ClientServerAuthenticationClient.cpp
 
#include <rw/secsock/RWSecureSocketContext.h>
#include <rw/secsock/RWSecureSocketPackageInit.h>
#include <rw/secsock/RWSecureSocketPortal.h>
 
#include <rw/network/RWInetAddr.h>
#include <rw/network/RWWinSockInfo.h>
 
#include <iostream>
#include <fstream>
 
#include "args.h"
#include "secsockexampledefs.h"
 
using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;
 
int main(int argc, char **argv)
{
try {
RWWinSockInfo info;
RWSecureSocketPackageInit secsockInit;
#if defined(RW_SECSOCK_RNG_NEEDS_SEEDING)
RWSecureSocketPackageInit::seedRNGFromFile(SEED_DATA_FILE);
#endif
int port = parseClientCommandLine(argc, argv);
 
ifstream certf(CLIENT_CERT_FILE); //1
RWX509Certificate cert(certf);
ifstream keyf(CLIENT_PRIVATE_KEY_FILE); //2
RWPrivateKey key(keyf);
RWSecureSocketContext context;
context.prepareToAuthenticate(TRUSTED_CERTS_FILE); //3
context.setIdentity(cert,key); //4
 
RWInetAddr addr(port, "localhost");
RWSecureSocketPortal portal(addr, context);
 
portal.sendAtLeast("Hello World!");
}
catch (const RWxmsg& m) {
cerr << m.why() << endl;
}
return 0;
}
//1 Open the PEM file that contains the client’s certificate, and create the certificate object.
//2 Open the PEM file that contains the client’s private key, and create the key object.
//3 Set up the context so that it authenticates the server using the certificates trusted by the client.
//4 Establish the identity of this client so that the server can verify its identity.