Writing a Client Program That Uses RWSecureSocketPortal
Example 2 is a client program that uses a portal to create a secure socket and send a message. This client is identical to the client in Example 1, except that it uses the Portal layer classes.
This example uses utility functions provided in args.h.
Example 2 – Client program that uses RWSecureSocketPortal
// File: examples\secsock\manual\RWSecureSocketPortalSimpleClient.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 "args.h"
#include "secsockexampledefs.h"
 
using std::cout;
using std::cerr;
using std::endl;
 
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);
 
RWSecureSocketContext context;
context.prepareToAuthenticate(TRUSTED_CERTS_FILE);
RWInetAddr addr(port, "localhost");
RWSecureSocketPortal portal(addr, context); //1
 
portal.sendAtLeast("Hello World!"); //2
 
// portal goes out of scope here //3
} catch(const RWInternalErr& ie) {
cerr << ie.why() << endl;
return 1;
} catch(const RWExternalErr& ee) {
cerr << ee.why() << endl;
return 1;
}
 
return 0;
}
//1 Creates a portal to the specified address using the specified context. The portal automatically creates a socket and connects it to the address.
//2 Sends a message.
//3 Closes the underlying socket automatically, as long as no other part of the program is holding a copy of the portal. The destructors for RWWinSockInfo and RWSecureSocketPackageInit are also executed here to clean up the Windows socket library and the underlying cryptographic library.