Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

CSecureSocket example

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

Contents

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, " 63.245.209.11");
 
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,
KAfInet,
KSockStream,
KProtocolInetTcp ) );
 
// Initiate socket connection
iSocket.Connect( iAddress, iStatus );
SetActive();
}
}

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

Contrib 018 i01 handshake.jpg

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

The page request goes is as follows:

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

Following two functions are retrieving the server response:

GetServerResponseL()
ReadServerResponseL()

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 04:32.
62 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×