Using an already active connection on Symbian
| ID | Creation date | 08.02.2008 | |
| Platform | S60 3rd Ed. MR | Tested on devices | Nokia E90, N95 |
| Category | Symbian C++ | Subcategory | Networking |
| APIs | Classes | RHTTPSession RSocketServ RConnection TCommDbConnPref TConnectionInfoBuf | |
| Methods |
Overview
You can use already active RConnection to get connection to internet and you save resources and memory. RConnection has enumeration of the active connections. Source code below finds active connections and attach to selected by IAP. If there is no selected active connection we create new one. Errors are not handled in the source code.
MMP file
The following libraries are required:
- http.lib
- ecom.lib
- esock.lib
- commdb.lib
Required Capabilities
NetworkServices
Header file
#include <http.h>
#include <es_sock.h>
#include <commdbconnpref.h>
Source file
// Selected IAP
TInt selectedIap=6;
// Open socket server and connection
RHTTPSession session; // TODO: Set to your class member variable
RSocketServ socketServ; // TODO: Set to your class member variable
RConnection connection; // TODO: Set to your class member variable
TCommDbConnPref connPref; // TODO: Set to your class member variable
socketServ.Connect();
connection.Open(iSocketServ);
// Search through existing connections.
// If there is already connection which matches to given IAP try to attach to
// existing connection.
TBool connected(EFalse);
TConnectionInfoBuf connInfo;
TUint count;
if ( connection.EnumerateConnections(count) == KErrNone )
{
for (TUint i=1; i<=count; i++)
{
// Note: GetConnectionInfo expects 1-based index
if ( connection.GetConnectionInfo( i, connInfo ) == KErrNone )
{
if ( connInfo().iIapId == selectedIap )
{
if ( connection.Attach(connInfo, RConnection::EAttachTypeNormal)
== KErrNone )
{
connected = ETrue;
break;
}
}
}
}
}
// Did we get active connection?
if ( !connected )
{
// Could not attach to existing connection.
// => Start a new connection.
connPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
connPref.SetIapId(selectedIap);
connection.Start(connPref);
}
// Open session
session.OpenL();
// Set the sessions connection info...
RStringPool strPool = session.StringPool();
RHTTPConnectionInfo connInfo = session.ConnectionInfo();
// ...to use our socket server
connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ,
RHTTPSession::GetTable() ), THTTPHdrVal (socketServ.Handle()) );
// ...to use our connection
connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketConnection,
RHTTPSession::GetTable() ),
THTTPHdrVal (REINTERPRET_CAST(TInt, &(connection))) );
// TODO: Remember to close handles in your class destructor like:
CYourClass::~CYourClass()
{
session.Close(); // Close handles in this order
connection.Close();
socketServ.Close();
}
See also
http://wiki.forum.nokia.com/index.php/TSS000056_-_How_can_I_determine_active_connections%3F

