Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Secure Communication Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

4.1 Getting Started with Secure Sockets

The following examples demonstrate some simple client and server programs using both the Communication Adapter Layer classes (through RWSecureSocket) and the Portal Layer classes (through RWSecureSocketPortal).

The examples in this section depend on the macros TRUSTED_CERTS_FILE, SERVER_DATA_FILE, SERVER_CERT_FILE, and SERVER_PRIVATE_KEY_FILE, which are defined in examples\secsock\secsockexampledefs.h. Several of the examples use these macros to determine certificates, servers, and ports.

4.1.1 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 util.h.

Example 1: Client program that uses RWSecureSocket

//1Constructs an RWWinSockInfo instance. Initializes the Winsock library under Windows, but has no effect under Unix.
//2Initializes the Secure Sockets package and the underlying cryptographic library.
//3Seeds 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 Section 4.4.1, "Seeding the Random Number Generator," for more information.
//4Constructs a context object that holds the default parameters for all secure sockets and portals that you create.
//5Gets 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 Section 4.3, "Obtaining Certificates."
//6Creates an address object that refers to the specified port on "localhost" (the local machine).
//7Creates a secure socket from the context
//8Connects the socket to the specified port on "localhost".
//9Sends a message over the socket.
//10Closes the socket.
//11Constructs destructors for RWWinSockInfo and RWSecureSocketPackageInit to clean up resources allocated by the underlying Winsock and cryptographic libraries.

4.1.2 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 util.h.

Example 2: Client program that uses RWSecureSocketPortal

//1Creates a portal to the specified address using the specified context. The portal automatically creates a socket and connects it to the address.
//2Sends a message.
//3Closes 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.

4.1.3 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 util.h.

Example 3: Server program that uses RWSecureSocket

//1Constructs an RWWinSockInfo instance. Initializes the Winsock library under Windows, but has no effect under Unix.
//2Initializes the Secure Sockets package and the underlying cryptographic library.
//3Seeds the random number generator, if necessary on your platform. Uses the data stored in SEED_DATA_FILE. See Section 4.4.1, "Seeding the Random Number Generator," for more information.
//4Constructs a context object that holds the default parameters for all sockets and portals that you create.
//5Constructs the server's certificate by using an istream to read the certificate from SERVER_CERT_FILE.
//6Constructs the server's private key by using an istream to read the key from SERVER_PRIVATE_KEY_FILE.
//7Associates the server's key and certificate with the context and specifies that this program will be acting as a server with the given identity.
//8Creates an address object that refers to the specified port on "localhost" (the local machine).
//9Creates a secure socket from the context
//10Binds the listener socket to the address and port.
//11Listens for connections and sets up a queue for them. Initializes the data structures.
//12Blocks until a connection request arrives.
//13Reads no more than 63 characters.
//14Displays the string received in line //13.
//15Closes the socket.
//16Executes destructors for RWWinSockInfo and RWSecureSocketPackageInit to clean up their associated libraries.

4.1.4 Writing a Server Program That Uses RWSecureSocketPortal

Example 4 is a server program that uses a portal to create a secure socket that waits for requests. Comments are included only for code that is different than Example 3.

This example uses utility functions provided in util.h.

Example 4: Server program that uses RWSecureSocketPortal

//1Initialize Windows Sockets library (does nothing on UNIX).
//2Initialize the Secure Sockets package.
//3Create a context instance to hold information related to the SSL connection.
//4Load the server's certificate.
//5Load the server's private key.
//6Associate the certificate and private key with the context.
//7Create an address instance which will be where the server listens for connections.
//8Create the listener instance passing the address to bind to and the context which contains the SSL connection information.
//9The function call operator (operator()) on a listener blocks and waits for a connection to arrive. Upon connection a portal to the newly created socket is returned.
//10Use the new portal. In this case we call recv() waiting for the client to send some data.
//11The data sent by the client is printed to standard output.

4.1.5 About the Keys and Certificates in the Example Programs

The keys and certificates supplied with the example programs were created using OpenSSL. They are located in the examples\certs directory. In subdirectories of that directory, readme.txt files describe the certificate infrastructure used for the examples and the scripts used to create them.


NOTE -- The scripts are provided on an as-is basis. Technical support will not be provided for them. Rogue Wave does support the example programs.


Previous fileTop of DocumentContentsIndex pageNext file

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.