Using Secure Sockets : Tutorial

This topic describes how to use secure sockets.

  1. Open a socket using RSocket::Open() function and connect it with RSocket::Connect() function.

  2. Create a secure socket by calling CSecureSocket::NewL() function with the parameters as socket and secure protocol name.

  3. To start the application acting as a client, use the CSecureSocket::StartClientHandshake() function to initiate a handshake with the remote server. To start the application acting as a server, use the CSecureSocket::StartServerHandshake() function. The call completes with an error code, if the handshake fails.

Secure Sockets example

In the following example iSocket is a reference to the already opened and connected socket and KSSLProtocol is the descriptor containing the name of a protocol, in this case TLS1.0. The function should return a pointer to the CSecureSocket.

       
        
       
       // Connect the socket server
   User::LeaveIfError(iSocketServ.Connect());
// Open the socket
   User::LeaveIfError(iSocket.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp)); 
//Connect the socket
   connectInetAddr.SetAddress(KTestAddress);
   connectInetAddr.SetPort(KSSLPort);  //TLS port

   iSocket.Connect(connectInetAddr, iStatus); 

   ...

    
// Construct the Tls socket
   iTlsSocket = CSecureSocket::NewL(iSocket, KSSLProtocol());

// start the handshake 
   iTlsSocket->StartClientHandshake(iStatus);
      

Use CSecureSocket::Send() to send data over the socket. Use CSecureSocket::Recv() and CSecureSocket::RecvOneOrMore() to receive data from the socket.