如何使用已连接的接入点

来源:互联网 发布:编程小白 编辑:程序博客网 时间:2024/04/29 18:06

简介

你可以使用已存在的连接RConnection,用来建立到互联网的连接。这将会节省资源和内存。

RConnection可以列出已存在的连接。示例代码中我们查找已存在的连接,并通过IAP接入。如果没有选择激活的连接,那么就会创建一个新的。在示例代码中没有处理错误信息。

MMP文件

LIBRARY http.lib

LIBRARY ecom.lib

LIBRARY esock.lib

LIBRARY commdb.lib

CAPABILITY NetworkServices


头文件

C/C++代码
  1. #include <http.h>  
  2. #include <es_sock.h>  
  3. #include <commdbconnpref.h>  


Source file

C/C++代码
  1. // Selected IAP  
  2. TInt selectedIap=6;  
  3.    
  4. // Open socket server and connection  
  5. RHTTPSession session;     // TODO: Set to your class member variable  
  6. RSocketServ socketServ;   // TODO: Set to your class member variable  
  7. RConnection connection;   // TODO: Set to your class member variable  
  8. TCommDbConnPref connPref; // TODO: Set to your class member variable  
  9. socketServ.Connect();  
  10. connection.Open(iSocketServ);  
  11.    
  12. // Search through existing connections.  
  13. // If there is already a connection that matches the given IAP, try to attach to  
  14. // existing connection.  
  15. TBool connected(EFalse);  
  16. TConnectionInfoBuf connInfo;  
  17. TUint count;  
  18. if ( connection.EnumerateConnections(count) == KErrNone )  
  19.     {  
  20.     for (TUint i=1; i<=count; i++)  
  21.     {  
  22.         // Note: GetConnectionInfo expects 1-based index  
  23.     if ( connection.GetConnectionInfo( i, connInfo ) == KErrNone )  
  24.         {  
  25.         if ( connInfo().iIapId == selectedIap )  
  26.         {  
  27.         if ( connection.Attach(connInfo, RConnection::EAttachTypeNormal)  
  28.         == KErrNone )  
  29.             {  
  30.             connected = ETrue;  
  31.             break;  
  32.             }  
  33.         }  
  34.         }  
  35.     }  
  36.     }  
  37.    
  38. // Is there an active connection?  
  39. if ( !connected )  
  40.     {  
  41.     // Could not attach to the existing connection.  
  42.     // => Start a new connection.  
  43.     connPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);  
  44.     connPref.SetIapId(selectedIap);  
  45.     connection.Start(connPref);  
  46.     }  
  47.    
  48. // Open session  
  49. session.OpenL();  
  50.    
  51. // Set the session's connection info...  
  52. RStringPool strPool = session.StringPool();  
  53. RHTTPConnectionInfo connInfo = session.ConnectionInfo();  
  54.    
  55. // ...to use the socket server  
  56. connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ,  
  57.     RHTTPSession::GetTable() ), THTTPHdrVal (socketServ.Handle()) );  
  58.    
  59. // ...to use the connection  
  60. connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketConnection,  
  61.     RHTTPSession::GetTable() ),   
  62.     THTTPHdrVal (REINTERPRET_CAST(TInt, &(connection))) );  
  63.    
  64.    
  65.    
  66.    
  67. // TODO: Remember to close handles in your class destructor, such as:  
  68. CYourClass::~CYourClass()  
  69.     {  
  70.     session.Close(); // Close handles in this order  
  71.     connection.Close();  
  72.     socketServ.Close();  
  73.     } 

原创粉丝点击