Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Can you get which source port the client has connected from?

Discussion in 'Unity Transport' started by inghy_, May 22, 2023.

  1. inghy_

    inghy_

    Joined:
    Sep 15, 2021
    Posts:
    5
    As stated in the FAQs, clients, each time they connect to a server, specifying their port, are given an Ephemeral port, that is each time different, for their local endpoint. This is comparable to having a UdpClient object with empty constructor.

    Once sent the first udp packets to the server, is there a way to know from the client which local port (i know the remote port may be different) has it bound to?

    I tried by using
    Code (CSharp):
    1. myUtpObject.ConnectionData.ListenEndPoint.Port
    but doesn't seem to be considered at all by clients
     
  2. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    Using Netcode for GameObjects (NGO), I don't think this is possible. The Unity Transport package itself offers this through the
    NetworkDriver.GetLocalEndpoint
    API, but unfortunately that's not exposed through the wrapper in NGO.

    If I may ask, why do you need access to this information?
     
  3. inghy_

    inghy_

    Joined:
    Sep 15, 2021
    Posts:
    5
    Basically, to open a first connection between peers (a NAT punch-through implementation), by sending a packet from the same port to a node server.
     
  4. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    Thanks for the answer. That seems like a very sensible use case. I'll file something on our end to expose this information. In the meantime, you can provide your own extension to
    UnityTransport
    to expose the functionality.

    Create a script with the following contents:
    Code (CSharp):
    1. using Unity.Networking.Transport;
    2.  
    3. namespace Unity.Netcode.Transports.UTP
    4. {
    5.     public partial class UnityTransport
    6.     {
    7.         public ushort GetLocalPort()
    8.         {
    9.             // Use GetLocalEndpoint instead if using Transport 2.0.
    10.             return m_Driver.LocalEndPoint().Port;
    11.         }
    12.     }
    13. }
    And then create an assembly reference so that this script gets included in the
    Unity.Netcode.Runtime
    assembly. You should then be able to use this new method to get the local port being used (of course this will only be available after the client has started).
     
    Last edited: May 24, 2023
    inghy_ likes this.
  5. inghy_

    inghy_

    Joined:
    Sep 15, 2021
    Posts:
    5

    Thank you, the extension works as expected!
    In the meantime i managed to get this port by checking every udp listener's port before and after setting up connection (not really reliable, even if it worked out), but this extension is a much cleaner solution.