Is it possible to use C++ for network programming in a Flutter app?

486

Yes, you can use dart:ffi (foreign function interface) to interface with native C++ code.

Learn more.

Share:
486
Ritwik
Author by

Ritwik

Updated on December 15, 2022

Comments

  • Ritwik
    Ritwik over 1 year

    I'm trying to build a mobile app with Dart and Flutter. But, trying to use C++ for networking.

    Q1: Is it possible to use C++ for socket programming, in a Flutter app?

    Q2: Does it run natively as C++ compiled code, or Flutter compiles it into Java/Swift in the final application.

    I haven't worked with flutter before (neither native Android/IOS), so this might be a naive question.

    • Richard Heap
      Richard Heap over 4 years
      Why wouldn't you use the dart:io classes for UDP, TCP and TLS sockets?
    • Ritwik
      Ritwik over 4 years
      I have already written some code for data encryption in C++. So, I directly wanted to implement encryption and data upload at one go.
    • Richard Heap
      Richard Heap over 4 years
      Unless it's immensely complicated you might want to port the C++ code to Dart. There's a fairly comprehensive suite of standard cryptography components in the pointycastle package you could leverage. Dart compiles to native code in release mode. There's a chance that that's less complex than learning dart:ffi. To answer your questions: Q1 - my guess would be no (or take a lot of work), but try it. Q2 - using ffi it would run natively in the Dart threads, which are different from the native Java/Swift threads. See also: github.com/flutter/flutter/wiki/The-Engine-architecture
  • Nicolas Cordova
    Nicolas Cordova over 4 years
    Upvoting because I didn't know what ffi meant. Thanks
  • Richard Heap
    Richard Heap over 4 years
    Sure, you might use ffi for some existing, computationally expensive C code (e.g. codec, encryption, image processing) but what would be the advantage for sockets? If you need native specific features (e.g. binding to a particular NIC) wouldn't you use a plugin to access the Android/iOS specific features?
  • creativecreatorormaybenot
    creativecreatorormaybenot over 4 years
    @RichardHeap Sure, the Dart implementations should be fine as well.