×
Namespaces

Variants
Actions
(Difference between revisions)

Using an already active connection on Symbian

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
Forum Nokia KB (Talk | contribs)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
__NOEDITSECTION__
 
__NOEDITSECTION__
 
+
{{KBCS}}
 
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"  
 
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"  
 
|-
 
|-
|'''ID''' || 
+
|'''ID''' ||CS000825
|'''Creation date''' ||08.02.2008
+
|'''Creation date''' ||February 15, 2008
 
|-
 
|-
|'''Platform''' ||S60 3rd Ed. MR
+
|'''Platform''' ||S60 3rd Edition, FP1
|'''Tested on devices''' ||Nokia E90, N95
+
|'''Tested on devices''' ||Nokia E90 Communicator, Nokia N95
 
|-
 
|-
 
|'''Category''' ||Symbian C++
 
|'''Category''' ||Symbian C++
Line 22: Line 22:
 
==Overview==
 
==Overview==
  
You can use already active <tt>RConnection</tt> to get connection to internet and you save resources and memory.  
+
You can use an already active <tt>RConnection</tt> to establish a connection to the Internet. This saves resources and memory.  
<tt>RConnection</tt> 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.
+
 
 +
<tt>RConnection</tt> has enumeration of the active connections. The source code finds active connections and attaches to the selected connection via the IAP. If there is no selected active connection, a new one is created. Errors are not handled in the source code.
  
 
==MMP file==
 
==MMP file==
  
The following libraries are required:
+
LIBRARY      http.lib
  
* http.lib
+
LIBRARY      ecom.lib
* ecom.lib
+
 
* esock.lib
+
LIBRARY      esock.lib
* commdb.lib
+
 
 +
LIBRARY      commdb.lib
 +
 
 +
CAPABILITY  NetworkServices
  
==Required Capabilities==
 
NetworkServices
 
  
 
==Header file==
 
==Header file==
Line 44: Line 46:
 
#include <commdbconnpref.h>
 
#include <commdbconnpref.h>
 
</code>  
 
</code>  
 +
  
 
==Source file==
 
==Source file==
Line 60: Line 63:
  
 
// Search through existing connections.
 
// Search through existing connections.
// If there is already connection which matches to given IAP try to attach to
+
// If there is already a connection that matches the given IAP, try to attach to
 
// existing connection.
 
// existing connection.
 
TBool connected(EFalse);
 
TBool connected(EFalse);
Line 85: Line 88:
 
     }
 
     }
  
// Did we get active connection?
+
// Is there an active connection?
 
if ( !connected )
 
if ( !connected )
 
     {
 
     {
     // Could not attach to existing connection.
+
     // Could not attach to the existing connection.
 
     // => Start a new connection.
 
     // => Start a new connection.
 
     connPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
 
     connPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
Line 98: Line 101:
 
session.OpenL();
 
session.OpenL();
  
// Set the sessions connection info...
+
// Set the session's connection info...
 
RStringPool strPool = session.StringPool();
 
RStringPool strPool = session.StringPool();
 
RHTTPConnectionInfo connInfo = session.ConnectionInfo();
 
RHTTPConnectionInfo connInfo = session.ConnectionInfo();
  
// ...to use our socket server
+
// ...to use the socket server
 
connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ,
 
connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ,
 
     RHTTPSession::GetTable() ), THTTPHdrVal (socketServ.Handle()) );
 
     RHTTPSession::GetTable() ), THTTPHdrVal (socketServ.Handle()) );
  
// ...to use our connection
+
// ...to use the connection
 
connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketConnection,
 
connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketConnection,
 
     RHTTPSession::GetTable() ),  
 
     RHTTPSession::GetTable() ),  
Line 114: Line 117:
  
  
// TODO: Remember to close handles in your class destructor like:
+
// TODO: Remember to close handles in your class destructor, such as:
 
CYourClass::~CYourClass()
 
CYourClass::~CYourClass()
 
     {
 
     {
Line 125: Line 128:
  
 
==See also==
 
==See also==
http://wiki.forum.nokia.com/index.php/TSS000056_-_How_can_I_determine_active_connections%3F
+
[[TSS000056 - How can I determine active connections?]]
  
 
[[Category:Symbian C++]][[Category:Networking]][[Category:Code Examples]]
 
[[Category:Symbian C++]][[Category:Networking]][[Category:Code Examples]]

Revision as of 15:04, 15 February 2008


Template:KBCS

ID CS000825 Creation date February 15, 2008
Platform S60 3rd Edition, FP1 Tested on devices Nokia E90 Communicator, Nokia N95
Category Symbian C++ Subcategory Networking
APIs   Classes RHTTPSession
RSocketServ
RConnection
TCommDbConnPref
TConnectionInfoBuf
Methods      

Overview

You can use an already active RConnection to establish a connection to the Internet. This saves resources and memory.

RConnection has enumeration of the active connections. The source code finds active connections and attaches to the selected connection via the IAP. If there is no selected active connection, a new one is created. Errors are not handled in the source code.

MMP file

LIBRARY http.lib

LIBRARY ecom.lib

LIBRARY esock.lib

LIBRARY commdb.lib

CAPABILITY 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 a connection that matches the 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;
}
}
}
}
}
 
// Is there an active connection?
if ( !connected )
{
// Could not attach to the existing connection.
// => Start a new connection.
connPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
connPref.SetIapId(selectedIap);
connection.Start(connPref);
}
 
// Open session
session.OpenL();
 
// Set the session's connection info...
RStringPool strPool = session.StringPool();
RHTTPConnectionInfo connInfo = session.ConnectionInfo();
 
// ...to use the socket server
connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ,
RHTTPSession::GetTable() ), THTTPHdrVal (socketServ.Handle()) );
 
// ...to use the connection
connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketConnection,
RHTTPSession::GetTable() ),
THTTPHdrVal (REINTERPRET_CAST(TInt, &(connection))) );
 
 
 
 
// TODO: Remember to close handles in your class destructor, such as:
CYourClass::~CYourClass()
{
session.Close(); // Close handles in this order
connection.Close();
socketServ.Close();
}

See also

TSS000056 - How can I determine active connections?

80 page views in the last 30 days.