Lesson 4: Add synchronization to your application

This lesson synchronizes your application with a consolidated database running on your computer.

The following procedures add synchronization code to your application, start the MobiLink server, and run your application to synchronize.

The UltraLite database you created in the previous lessons synchronizes with the UltraLite 11 Sample database. The UltraLite 11 Sample database has a ULCustomer table whose columns include those in the customer table of your local UltraLite database.

This lesson assumes that you are familiar with MobiLink synchronization.

Add synchronization to your application
  1. Add the method below to customer.cpp. This method carries out the following tasks:

    • Sets the synchronization stream to TCP/IP by invoking ULEnableTcpipSynchronization. Synchronization can also be carried out over HTTP, HotSync, or HTTPS. See UltraLite clients.

    • Sets the script version. MobiLink synchronization is controlled by scripts stored in the consolidated database. The script version identifies which set of scripts to use.

    • Sets the MobiLink user name. This value is used for authentication at the MobiLink server. It is distinct from the UltraLite database user ID, although in some applications you may want to give them the same value.

    • Sets the download_only parameter to true. By default, MobiLink synchronization is two-way. This application uses download-only synchronization so that the rows in your table do not get uploaded to the sample database.

    bool do_sync( Connection * conn ) {
      ul_synch_info info;
      ul_stream_error * se = &info.stream_error;
      
      ULEnableTcpipSynchronization( Tutca.GetCA() );
      conn->InitSynchInfo( &info );
      info.stream = ULSocketStream();
      info.version = UL_TEXT( "custdb 11.0" );
      info.user_name = UL_TEXT( "50" );
      info.download_only = true;
      if( !conn->Synchronize( &info ) ) {
          _tprintf( _TEXT("Synchronization error \n" ));   
       _tprintf( _TEXT("  stream_error_code is '%lu'\n"), se->stream_error_code );
       _tprintf( _TEXT("  system_error_code is '%ld'\n"), se->system_error_code );
       _tprintf( _TEXT("  error_string is '") );
       _tprintf( _TEXT("%s"), se->error_string );
       _tprintf( _TEXT("'\n") );
       return false;
      }
      return true;
    }
  2. Add the following line to the main method, immediately after the call to the insert method and before the call to the select method:

    do_sync(conn);
  3. Compile your application by running nmake.

Synchronize data
  1. Start the MobiLink server.

    From a command prompt, run the following command:

    mlsrv11 -c "dsn=SQL Anywhere 11 CustDB" -v+ -zu+

    The -zu+ option provides automatic addition of users. The -v+ option turns on verbose logging for all messages.

    For more information about this option, see MobiLink server options.

  2. Run your application by typing customer at the command prompt.

    The MobiLink server window displays status messages indicating the synchronization progress. If synchronization is successful, the final message displays Synchronization complete.