CSecureSocket example

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleArticle
Created: Carbider (29 Nov 2007)
Last edited: hamishwillee (30 May 2013)


Introduction and external links

The example is based on SSLExampleCode but is simplified and is put in operational status.

The example connection goes to http://www.mozilla.org/

Necessary libraries

To operate with CSecureSocket class we need to include this header files:

#include <in_sock.h>
#include <SecureSocket.h>

And to add this libraries to .mmp file:

LIBRARY insock.lib
LIBRARY securesocket.lib
LIBRARY esock.lib

Connection process

The connection is organized as Active Object. In ConnectL() method the RSocket socket is opened and connected:

void CSecureSocketCore::ConnectL()
_LIT( KIPAddress, "");
TInetAddr addr;
TUint32 aAddr;
iState = EMakingSecureConnection;
if ( addr.Input( KIPAddress ) == KErrNone )
// server name is already a valid ip address
aAddr = addr.Address();
iAddress.SetPort( iPort );
iAddress.SetAddress( aAddr );
iAddress.SetFamily( KAfInet );
// Open a TCP socket
User::LeaveIfError( iSocket.Open( iSocketServer,
KProtocolInetTcp ) );
// Initiate socket connection
iSocket.Connect( iAddress, iStatus );

Then MakeSecureConnectionL() method accomplishes a handshake with the server:

Contrib 018 i01 handshake.jpg

void CSecureSocketCore::MakeSecureConnectionL()
iTlsSocket = CSecureSocket::NewL( iSocket, _L("SSL3.0"));
// start the handshake
iTlsSocket->StartClientHandshake( iStatus );

The page request goes is as follows:

void CSecureSocketCore::MakePageRequestL()
_LIT8( KPage , "http://www.mozilla.org/");
// Create a GET request
iSndBuffer+=KSimpleGet; // "GET "
iSndBuffer+=KNewLine; // "\n"
// Send the request
iTlsSocket->Send( iSndBuffer, iStatus, iBytesSent );

Following two functions are retrieving the server response:


Contrib 018 i02 output.gif

Download the example source code

The example is available as Carbide C++ project. To know how to import project from Zip look here. Download Zip file from here: File:WikiSecureSocketEx.zip

The main implementation is in the SecureSocketCore.cpp file

This page was last modified on 30 May 2013, at 07:32.
68 page views in the last 30 days.