Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Using c++ sockets in Unity with a native plugin

Discussion in 'Scripting' started by Afropenguinn, Aug 14, 2017.

  1. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    I made a cross platform (Linux, Windows, Mac) UPDSocket class in c++ that I am using for a server. It sends and receives fine, I've tested it cross platform, ect. But now I'm wondering if I would be able to implement it into Unity as a native plugin, to reuse code and work from a single code base, or if I'd be better off using a separate System.Net.Sockets solution?
     
  2. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    I know I'm answering my own question here, but it seems like using a System.Net.Sockets implementation is way simpler. Because c++ and C# have a vastly different concept of what a class is, you can't simply convert one to the other. Well, correction, you CAN, but it seems like a lot of hassle to go through when C# has a perfectly good socket implementation built in.
     
  3. dpgdemos

    dpgdemos

    Joined:
    Apr 28, 2014
    Posts:
    24
    To use native code in .NET, you first have to create a managed class wrapper. From there, you can import the DLL files into your Plugins folder under Assets. While doable, it's a lot of work to accomplish what .NET has already accomplished for you with UDP sockets. If it was me, I'd just stick to .NET unless there's lower level optimizations that need to be obtained.