Writing a Server Program That Uses RWSecureSocket
Example 3 is a server program that creates a secure socket that waits for requests.
This example uses utility functions provided in args.h.
Example 3 – Server program that uses RWSecureSocket
// File: examples\secsock\manual\RWSecureSocketSimpleServer.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 <fstream>
#include <iostream>
 
#include "args.h"
#include "secsockexampledefs.h"
 
using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
 
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 = parseCommandLine(argc, argv);
 
RWSecureSocketContext context; //4
 
ifstream certf(SERVER_CERT_FILE);
RWX509Certificate cert(certf); //5
ifstream keyf(SERVER_PRIVATE_KEY_FILE);
RWPrivateKey key(keyf); //6
context.setIdentity(cert, key); //7
 
RWInetAddr addr(port, "localhost"); //8
RWSecureSocket listenSock(context); //9
listenSock.bind(addr); //10
listenSock.listen(); //11
 
// Display the port actually bound to
cout << listenSock.getsockname() << endl;
 
char buf[64];
for(volatile bool spin = true; spin ; /* */) {
RWSecureSocket newSock = listenSock.accept(); //12
buf[newSock.recv(buf, 63)] = 0; //13
cout << "Received: " << buf << endl; //14
newSock.close(); //15
}
}
catch (const RWxmsg& m) {
cerr << m.why() << endl;
return 1;
}
return 0; //16
}
//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 necessary on your platform. Uses the data stored in SEED_DATA_FILE. See Seeding the Random Number Generator for more information.
//4 Constructs a context object that holds the default parameters for all sockets and portals that you create.
//5 Constructs the server’s certificate by using an istream to read the certificate from SERVER_CERT_FILE.
//6 Constructs the server’s private key by using an istream to read the key from SERVER_PRIVATE_KEY_FILE.
//7 Associates the server’s key and certificate with the context and specifies that this program will be acting as a server with the given identity.
//8 Creates an address object that refers to the specified port on "localhost" (the local machine).
//9 Creates a secure socket from the context
//10 Binds the listener socket to the address and port.
//11 Listens for connections and sets up a queue for them. Initializes the data structures.
//12 Blocks until a connection request arrives.
//13 Reads no more than 63 characters.
//14 Displays the string received in line //11.
//15 Closes the socket.
//16 Executes destructors for RWWinSockInfo and RWSecureSocketPackageInit to clean up their associated libraries.