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
}