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 System.Net.Sockets in my windows store build.

Discussion in 'Windows' started by Kulgann, Jul 6, 2015.

  1. Kulgann

    Kulgann

    Joined:
    Oct 17, 2014
    Posts:
    15
    The game already runs on iOS and Android. I want to port it to the windows store and I'm having trouble figuring out how get the sockets to work. Is there a plugin i can use or what?. I'm using them to get NTP time in this code:

    public static DateTime GetNetworkTime()
    {
    try
    {
    const string ntpServer = "pool.ntp.org";
    var ntpData = new byte[48];
    ntpData[0] = 0x1B; //LeapIndicator = 0 (no warning), VersionNum = 3 (IPv4 only), Mode = 3 (Client Mode)

    var addresses = Dns.GetHostEntry(ntpServer).AddressList;
    var ipEndPoint = new IPEndPoint(addresses[0], 123);
    var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);



    socket.Connect(ipEndPoint);
    socket.ReceiveTimeout = 2000;
    socket.Send(ntpData);
    socket.Receive(ntpData);



    socket.Close();

    ulong intPart = (ulong)ntpData[40] << 24 | (ulong)ntpData[41] << 16 | (ulong)ntpData[42] << 8 | (ulong)ntpData[43];
    ulong fractPart = (ulong)ntpData[44] << 24 | (ulong)ntpData[45] << 16 | (ulong)ntpData[46] << 8 | (ulong)ntpData[47];

    var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
    var networkDateTime = (new DateTime(1900, 1, 1)).AddMilliseconds((long)milliseconds);
    return networkDateTime;

    }
    catch (SocketException) { Debug.Log("Did not get date from NTP server!!"); return new DateTime(); }

    }
     
    Last edited: Jul 6, 2015
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,672
  3. Kulgann

    Kulgann

    Joined:
    Oct 17, 2014
    Posts:
    15
    Ty you for the anwser. I'm going to go with using the WinRT API. Do I need to add a plugin to the unity project or just a reference to the proper assemblies in the code ?
     
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,672
    If you're building for WSA or WP8.1, you can write directly in Unity script code, but need to wrap that code with #if NETFX_CORE / #endif, the needed assemblies are already referenced.

    You can also go with this approach, as it will let you quickly test and build the code:
    * In Unity scripts, add static delegates for Network functions you need
    * Set delegates from for ex., App.xaml.cs
    * Build & Run
    * When you'll execute Network code from Unity script, it will call into your callback from App.xaml.cs
    * If you need to tweak something, it will be enough to recompile VS solution.
     
  5. Kulgann

    Kulgann

    Joined:
    Oct 17, 2014
    Posts:
    15
    Ok. I got confused since I'm using visual studio tools to write my code directly in Vs2013 and when it wasnt auto-completing the Windows.(whatever) references I got worried. If i wrap them in #if NETFX_CORE I wont get compile errors and it will still run on the windows platforms. I think I got it now